This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgo.ts
More file actions
49 lines (43 loc) · 1.73 KB
/
go.ts
File metadata and controls
49 lines (43 loc) · 1.73 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 * as path from 'path'
import { slashPattern } from './comments'
import { FilterContext, LanguageSpec, Result, LSIFSupport } from './spec'
import { extractFromLines, filterResults } from './util'
/**
* Filter a list of candidate definitions to select those likely to be valid
* cross-references for a definition in this file. Accept candidates located
* in the same package or in a directory that includes one of the imported
* paths.
*
* If no candidates match, fall back to the raw (unfiltered) results so that
* the user doesn't get an empty response unless there really is nothing.
*/
function filterDefinitions<T extends Result>(results: T[], { repo, filePath, fileContent }: FilterContext): T[] {
const importPaths = extractFromLines(fileContent, /^(?:import |\t)(?:\w+ |\. )?"(.*)"$/)
return filterResults(results, ({ repo: resultRepo, file }) => {
const resultImportPath = importPath(resultRepo, file)
return (
// Match results from the same package
resultImportPath === importPath(repo, filePath) ||
// Match results that are imported explicitly
importPaths.some(index => resultImportPath.includes(index))
)
})
}
/**
* Return the Go import path for a particular file.
*
* @param repo The name of the repository.
* @param filePath The relative path to the file from the repo root.
*/
function importPath(repo: string, filePath: string): string {
return `${repo}/${path.dirname(filePath)}`
}
export const goSpec: LanguageSpec = {
languageID: 'go',
stylized: 'Go',
fileExts: ['go'],
commentStyles: [{ lineRegex: slashPattern }],
filterDefinitions,
lsifSupport: LSIFSupport.Robust,
textDocumentImplemenationSupport: true,
}