Skip to content

Commit 028cbd1

Browse files
authored
feat: support for finding symbol references with souregraph (conwnet#329)
1 parent 722a7b6 commit 028cbd1

6 files changed

Lines changed: 333 additions & 105 deletions

File tree

‎extensions/github1s/src/interfaces/sourcegraph/definition.ts‎

Lines changed: 4 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
*/
55

66
import { gql } from '@apollo/client/core';
7-
import {
8-
escapeRegexp,
9-
sourcegraphClient,
10-
getRepoRefQueryString,
11-
} from './common';
7+
import { sourcegraphClient } from './common';
8+
import { getSymbolPositions } from './position';
129

1310
export interface SymbolDefinition {
1411
precise: boolean;
@@ -108,85 +105,6 @@ const getLSIFDefinitions = async (
108105
});
109106
};
110107

111-
const searchDefinitionsQuery = gql`
112-
query($query: String!) {
113-
search(query: $query) {
114-
results {
115-
results {
116-
... on FileMatch {
117-
symbols {
118-
name
119-
kind
120-
location {
121-
resource {
122-
path
123-
repository {
124-
name
125-
}
126-
commit {
127-
oid
128-
}
129-
}
130-
range {
131-
start {
132-
line
133-
character
134-
}
135-
end {
136-
line
137-
character
138-
}
139-
}
140-
}
141-
}
142-
}
143-
}
144-
}
145-
}
146-
}
147-
`;
148-
149-
// get symbol definitions base on search
150-
export const getSearchDefinitions = async (
151-
owner: string,
152-
repo: string,
153-
ref: string,
154-
symbol: string
155-
): Promise<SymbolDefinition[]> => {
156-
const repoRefString = getRepoRefQueryString(owner, repo, ref);
157-
const optionsString = [
158-
'context:global',
159-
'type:symbol',
160-
'patternType:regexp',
161-
'case:yes',
162-
].join(' ');
163-
const patternString = `^${escapeRegexp(symbol)}$`;
164-
const query = [repoRefString, optionsString, patternString].join(' ');
165-
const response = await sourcegraphClient.query({
166-
query: searchDefinitionsQuery,
167-
variables: { query },
168-
});
169-
170-
const resultSymbols = response?.data?.search?.results?.results?.flatMap(
171-
(item) => item.symbols
172-
);
173-
return (resultSymbols || []).map((symbol) => {
174-
const { resource, range } = symbol.location;
175-
const [owner, repo] = resource.repository.name
176-
.split('/')
177-
.filter(Boolean)
178-
.slice(-2);
179-
return {
180-
precise: false,
181-
owner,
182-
repo,
183-
ref: resource.commit.oid,
184-
path: `/${resource.path}`,
185-
range,
186-
};
187-
});
188-
};
189-
190108
export const getSymbolDefinitions = (
191109
owner: string,
192110
repo: string,
@@ -208,17 +126,12 @@ export const getSymbolDefinitions = (
208126
line,
209127
character
210128
);
211-
const searchDefinitionsPromise = getSearchDefinitions(
212-
owner,
213-
repo,
214-
ref,
215-
symbol
216-
);
129+
const searchDefinitionsPromise = getSymbolPositions(owner, repo, ref, symbol);
217130

218131
return LSIFDefinitionsPromise.then((LSIFDefinitions) => {
219132
if (LSIFDefinitions.length) {
220133
return LSIFDefinitions;
221134
}
222-
return searchDefinitionsPromise;
135+
return searchDefinitionsPromise as Promise<SymbolDefinition[]>;
223136
});
224137
};
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @file Sourcegraph api for searching symbol positions
3+
* @author netcon
4+
*/
5+
6+
import { gql } from '@apollo/client/core';
7+
import {
8+
escapeRegexp,
9+
sourcegraphClient,
10+
getRepoRefQueryString,
11+
} from './common';
12+
13+
export interface SymbolPosition {
14+
precise: boolean;
15+
owner: string;
16+
repo: string;
17+
ref: string;
18+
path: string;
19+
range: {
20+
start: {
21+
line: number;
22+
character: number;
23+
};
24+
end: {
25+
line: number;
26+
character: number;
27+
};
28+
};
29+
}
30+
31+
const searchPositionsQuery = gql`
32+
query($query: String!) {
33+
search(query: $query) {
34+
results {
35+
results {
36+
... on FileMatch {
37+
symbols {
38+
name
39+
kind
40+
location {
41+
resource {
42+
path
43+
repository {
44+
name
45+
}
46+
commit {
47+
oid
48+
}
49+
}
50+
range {
51+
start {
52+
line
53+
character
54+
}
55+
end {
56+
line
57+
character
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
67+
`;
68+
69+
// get symbol position information base on search,
70+
// used by definition, reference and hover
71+
export const getSymbolPositions = async (
72+
owner: string,
73+
repo: string,
74+
ref: string,
75+
symbol: string
76+
): Promise<SymbolPosition[]> => {
77+
const repoRefString = getRepoRefQueryString(owner, repo, ref);
78+
const optionsString = [
79+
'context:global',
80+
'type:symbol',
81+
'patternType:regexp',
82+
'case:yes',
83+
].join(' ');
84+
const patternString = `^${escapeRegexp(symbol)}$`;
85+
const query = [repoRefString, optionsString, patternString].join(' ');
86+
const response = await sourcegraphClient.query({
87+
query: searchPositionsQuery,
88+
variables: { query },
89+
});
90+
91+
const resultSymbols = response?.data?.search?.results?.results?.flatMap(
92+
(item) => item.symbols
93+
);
94+
return (resultSymbols || []).map((symbol) => {
95+
const { resource, range } = symbol.location;
96+
const [owner, repo] = resource.repository.name
97+
.split('/')
98+
.filter(Boolean)
99+
.slice(-2);
100+
return {
101+
precise: false,
102+
owner,
103+
repo,
104+
ref: resource.commit.oid,
105+
path: `/${resource.path}`,
106+
range,
107+
};
108+
});
109+
};
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* @file Sourcegraph reference api
3+
* @author netcon
4+
*/
5+
6+
import { gql } from '@apollo/client/core';
7+
import { sourcegraphClient } from './common';
8+
import { getSymbolPositions } from './position';
9+
10+
export interface SymbolReference {
11+
precise: boolean;
12+
owner: string;
13+
repo: string;
14+
ref: string;
15+
path: string;
16+
range: {
17+
start: {
18+
line: number;
19+
character: number;
20+
};
21+
end: {
22+
line: number;
23+
character: number;
24+
};
25+
};
26+
}
27+
28+
const LSIFReferencesQuery = gql`
29+
query(
30+
$repository: String!
31+
$ref: String!
32+
$path: String!
33+
$line: Int!
34+
$character: Int!
35+
) {
36+
repository(name: $repository) {
37+
commit(rev: $ref) {
38+
blob(path: $path) {
39+
lsif {
40+
references(line: $line, character: $character) {
41+
nodes {
42+
resource {
43+
path
44+
repository {
45+
name
46+
}
47+
commit {
48+
oid
49+
}
50+
}
51+
range {
52+
start {
53+
line
54+
character
55+
}
56+
end {
57+
line
58+
character
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
67+
}
68+
`;
69+
70+
// find references with Sourcegraph LSIF
71+
// https://docs.sourcegraph.com/code_intelligence/explanations/precise_code_intelligence
72+
const getLSIFReferences = async (
73+
owner: string,
74+
repo: string,
75+
ref: string,
76+
path: string,
77+
line: number,
78+
character: number
79+
): Promise<SymbolReference[]> => {
80+
const response = await sourcegraphClient.query({
81+
query: LSIFReferencesQuery,
82+
variables: {
83+
repository: `github.com/${owner}/${repo}`,
84+
ref,
85+
path: path.slice(1),
86+
line,
87+
character,
88+
},
89+
});
90+
const referenceNodes =
91+
response?.data?.repository?.commit?.blob?.lsif?.references?.nodes;
92+
return (referenceNodes || []).map(({ resource, range }) => {
93+
const [owner, repo] = resource.repository.name
94+
.split('/')
95+
.filter(Boolean)
96+
.slice(-2);
97+
return {
98+
precise: true,
99+
owner,
100+
repo,
101+
ref: resource.commit.oid,
102+
path: `/${resource.path}`,
103+
range,
104+
};
105+
});
106+
};
107+
108+
export const getSymbolReferences = (
109+
owner: string,
110+
repo: string,
111+
ref: string,
112+
path: string,
113+
line: number,
114+
character: number,
115+
symbol: string
116+
): Promise<SymbolReference[]> => {
117+
// if failed to find references from LSIF,
118+
// fallback to search-based references, using
119+
// two promise instead of `await` to request in
120+
// parallel for getting result as soon as possible
121+
const LSIFReferencesPromise = getLSIFReferences(
122+
owner,
123+
repo,
124+
ref,
125+
path,
126+
line,
127+
character
128+
);
129+
const searchReferencesPromise = getSymbolPositions(owner, repo, ref, symbol);
130+
131+
return LSIFReferencesPromise.then((LSIFReferences) => {
132+
if (LSIFReferences.length) {
133+
return LSIFReferences;
134+
}
135+
return searchReferencesPromise as Promise<SymbolReference[]>;
136+
});
137+
};

0 commit comments

Comments
 (0)