-
Notifications
You must be signed in to change notification settings - Fork 874
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #334 from conwnet/master
release 0.5.0
- Loading branch information
Showing
33 changed files
with
2,252 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ lib | |
dist | ||
out | ||
node_modules | ||
**/src/vs/** | ||
vscode-web-github1s/src/vs | ||
vscode-web-github1s/extensions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* @file extension url helpers | ||
* @author netcon | ||
*/ | ||
|
||
export const getSourcegraphUrl = ( | ||
owner: string, | ||
repo: string, | ||
ref: string, | ||
path: string, | ||
line: number, | ||
character: number | ||
): string => { | ||
const repoUrl = `https://sourcegraph.com/github.com/${owner}/${repo}@${ref}`; | ||
return `${repoUrl}/-/blob${path}#L${line + 1}:${character + 1}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* @file Sourcegraph api common utils | ||
* @author netcon | ||
*/ | ||
|
||
import { | ||
ApolloClient, | ||
createHttpLink, | ||
InMemoryCache, | ||
} from '@apollo/client/core'; | ||
import { trimEnd, trimStart } from '@/helpers/util'; | ||
|
||
const sourcegraphLink = createHttpLink({ | ||
// Since the Sourcegraph refused the CORS check now, | ||
// use Vercel Serverless Function to proxy it temporarily | ||
// See `/api/sourcegraph.js` | ||
uri: '/api/sourcegraph', | ||
}); | ||
|
||
export const sourcegraphClient = new ApolloClient({ | ||
link: sourcegraphLink, | ||
cache: new InMemoryCache(), | ||
}); | ||
|
||
export const canBeConvertToRegExp = (str: string) => { | ||
try { | ||
new RegExp(str); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
|
||
export const combineGlobsToRegExp = (globs: string[]) => { | ||
// only support very simple globs convert now | ||
const result = Array.from( | ||
new Set( | ||
globs.map((glob: string) => | ||
trimEnd(trimStart(glob, '*/'), '*/').replace(/^\./, '\\.') | ||
) | ||
) | ||
) | ||
// if the glob still not can be convert to a regexp, just ignore it | ||
.filter((item) => canBeConvertToRegExp(item)) | ||
.join('|'); | ||
// ensure the result can be convert to a regexp | ||
return canBeConvertToRegExp(result) ? result : ''; | ||
}; | ||
|
||
export const escapeRegexp = (text: string): string => | ||
text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); | ||
|
||
export const getRepoRefQueryString = ( | ||
owner: string, | ||
repo: string, | ||
ref: string | ||
) => { | ||
// the string may looks like `^github\.com/conwnet/github1s$` | ||
const repoPattern = `^${escapeRegexp(`github\.com/${owner}/${repo}`)}$`; | ||
const repoRefQueryString = | ||
ref.toUpperCase() === 'HEAD' | ||
? `repo:${repoPattern}` | ||
: `repo:${repoPattern}@${ref}`; | ||
return repoRefQueryString; | ||
}; |
137 changes: 137 additions & 0 deletions
137
extensions/github1s/src/interfaces/sourcegraph/definition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/** | ||
* @file Sourcegraph definition api | ||
* @author netcon | ||
*/ | ||
|
||
import { gql } from '@apollo/client/core'; | ||
import { sourcegraphClient } from './common'; | ||
import { getSymbolPositions } from './position'; | ||
|
||
export interface SymbolDefinition { | ||
precise: boolean; | ||
owner: string; | ||
repo: string; | ||
ref: string; | ||
path: string; | ||
range: { | ||
start: { | ||
line: number; | ||
character: number; | ||
}; | ||
end: { | ||
line: number; | ||
character: number; | ||
}; | ||
}; | ||
} | ||
|
||
const LSIFDefinitionsQuery = gql` | ||
query( | ||
$repository: String! | ||
$ref: String! | ||
$path: String! | ||
$line: Int! | ||
$character: Int! | ||
) { | ||
repository(name: $repository) { | ||
commit(rev: $ref) { | ||
blob(path: $path) { | ||
lsif { | ||
definitions(line: $line, character: $character) { | ||
nodes { | ||
resource { | ||
path | ||
repository { | ||
name | ||
} | ||
commit { | ||
oid | ||
} | ||
} | ||
range { | ||
start { | ||
line | ||
character | ||
} | ||
end { | ||
line | ||
character | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
// find definitions with Sourcegraph LSIF | ||
// https://docs.sourcegraph.com/code_intelligence/explanations/precise_code_intelligence | ||
const getLSIFDefinitions = async ( | ||
owner: string, | ||
repo: string, | ||
ref: string, | ||
path: string, | ||
line: number, | ||
character: number | ||
): Promise<SymbolDefinition[]> => { | ||
const response = await sourcegraphClient.query({ | ||
query: LSIFDefinitionsQuery, | ||
variables: { | ||
repository: `github.com/${owner}/${repo}`, | ||
ref, | ||
path: path.slice(1), | ||
line, | ||
character, | ||
}, | ||
}); | ||
const definitionNodes = | ||
response?.data?.repository?.commit?.blob?.lsif?.definitions?.nodes; | ||
return (definitionNodes || []).map(({ resource, range }) => { | ||
const [owner, repo] = resource.repository.name | ||
.split('/') | ||
.filter(Boolean) | ||
.slice(-2); | ||
return { | ||
precise: true, | ||
owner, | ||
repo, | ||
ref: resource.commit.oid, | ||
path: `/${resource.path}`, | ||
range, | ||
}; | ||
}); | ||
}; | ||
|
||
export const getSymbolDefinitions = ( | ||
owner: string, | ||
repo: string, | ||
ref: string, | ||
path: string, | ||
line: number, | ||
character: number, | ||
symbol: string | ||
): Promise<SymbolDefinition[]> => { | ||
// if failed to find definitions from LSIF, | ||
// fallback to search-based definitions, using | ||
// two promise instead of `await` to request in | ||
// parallel for getting result as soon as possible | ||
const LSIFDefinitionsPromise = getLSIFDefinitions( | ||
owner, | ||
repo, | ||
ref, | ||
path, | ||
line, | ||
character | ||
); | ||
const searchDefinitionsPromise = getSymbolPositions(owner, repo, ref, symbol); | ||
|
||
return LSIFDefinitionsPromise.then((LSIFDefinitions) => { | ||
if (LSIFDefinitions.length) { | ||
return LSIFDefinitions; | ||
} | ||
return searchDefinitionsPromise as Promise<SymbolDefinition[]>; | ||
}); | ||
}; |
Oops, something went wrong.
f8b45d6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: