Skip to content

Instantly share code, notes, and snippets.

@chrismwendt
Created November 7, 2018 21:52
Show Gist options
  • Save chrismwendt/9d6437de9f7c7b0a7e033de30b5166e9 to your computer and use it in GitHub Desktop.
Save chrismwendt/9d6437de9f7c7b0a7e033de30b5166e9 to your computer and use it in GitHub Desktop.
Coverrts from LSP types to Sourcegraph extension API types
import * as lsp from 'vscode-languageserver-protocol'
import * as sourcegraph from 'sourcegraph'
import { Location, Position, Range } from 'sourcegraph'
const lspToSEA = {
location: ({
currentDocURI,
location: { range, uri: uriFromLangServer },
}: {
currentDocURI: string
location: lsp.Location
}): Location => {
let definitionURI: sourcegraph.URI
if (/^file:\/\/\//.test(uriFromLangServer)) {
// The definition is in a file in the same repo
const docURL = new URL(currentDocURI)
docURL.hash = uriFromLangServer.slice('file:///'.length)
definitionURI = new sourcegraph.URI(docURL.href)
} else {
definitionURI = new sourcegraph.URI(uriFromLangServer)
}
return new Location(
definitionURI,
range &&
new Range(
new Position(range.start.line, range.start.character),
new Position(range.end.line, range.end.character)
)
)
},
definition: ({
currentDocURI,
definition,
}: {
currentDocURI: string
definition: Definition
}): sourcegraph.Definition => {
if (!definition) {
return null
}
if (Array.isArray(definition)) {
return definition.map(location => lspToSEA.location({ currentDocURI: currentDocURI, location }))
} else {
const location = definition
return lspToSEA.location({ currentDocURI: currentDocURI, location })
}
},
references: ({
currentDocURI,
references,
}: {
currentDocURI: string
references: lsp.Location[] | null
}): Location[] => {
if (!references) {
return []
}
return references.map(location => lspToSEA.location({ currentDocURI: currentDocURI, location }))
},
hover: hover => {
if (!hover) {
return null
}
return {
contents: { value: '' },
__backcompatContents: hover.contents,
} as sourcegraph.Hover
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment