forked from jasonnutter/vscode-codeowners
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcodeowners-hover-provider.ts
More file actions
49 lines (44 loc) · 1.29 KB
/
codeowners-hover-provider.ts
File metadata and controls
49 lines (44 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import vscode from "vscode"
import { dirname } from "path"
import fs from "fs"
export class CodeownersHoverProvider implements vscode.HoverProvider {
provideHover(
document: vscode.TextDocument,
position: vscode.Position,
): vscode.ProviderResult<vscode.Hover> {
const line = document.lineAt(position.line)
const m = line.text.match(/^\s*(\S+)/)?.[1]
if (m == null) {
return { contents: [] }
}
const idx = line.text.indexOf(m)
const workspaceDir = dirname(dirname(document.uri.fsPath))
const myPath = workspaceDir + "/" + m
let isDirectory: boolean | null = null
try {
isDirectory = fs.statSync(myPath).isDirectory()
} catch (e) {
// @ts-expect-error we should see this error.
if (e.code !== "ENOENT") {
console.error("github-code-owners", e)
}
}
const x = new vscode.MarkdownString()
x.appendCodeblock(m)
const isPattern = !m.startsWith("/")
const range = new vscode.Range(
new vscode.Position(position.line, idx),
new vscode.Position(position.line, idx + m.length),
)
if (!range.contains(position)) {
return { contents: [] }
}
return {
range,
contents: [
x,
isPattern ? "Substring pattern match" : `Matches path exactly`,
],
}
}
}