Skip to content
This repository was archived by the owner on Feb 22, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@
"typescript": "^3.1.6"
},
"dependencies": {
"@sourcegraph/basic-code-intel": "6.0.14",
"@sourcegraph/basic-code-intel": "6.0.15",
"@sourcegraph/vscode-ws-jsonrpc": "0.0.3-fork",
"path-browserify": "^1.0.0",
"prettier": "^1.16.4",
"rxjs": "^6.3.3",
"sourcegraph-langserver-http": "https://github.com/sourcegraph/sourcegraph-langserver-http#0b0173feef37d1f4f68d881c95ac3f4c97bfedb3",
Expand Down
190 changes: 55 additions & 135 deletions src/lang-go.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '@babel/polyfill'

import * as basicCodeIntel from '@sourcegraph/basic-code-intel'
import { activateBasicCodeIntel } from '@sourcegraph/basic-code-intel'
import * as wsrpc from '@sourcegraph/vscode-ws-jsonrpc'
import { ajax } from 'rxjs/ajax'
import * as sourcegraph from 'sourcegraph'
Expand All @@ -10,19 +10,7 @@ import * as convert from './convert-lsp-to-sea'
import * as lspext from './lspext'

import * as path from 'path'

import {
BehaviorSubject,
from,
Observable,
Observer,
of,
throwError,
race,
combineLatest,
ObservableInput,
merge,
} from 'rxjs'
import { BehaviorSubject, from, Observable, Observer, of, throwError } from 'rxjs'
import {
concatMap,
distinctUntilChanged,
Expand All @@ -33,20 +21,11 @@ import {
switchMap,
take,
finalize,
tap,
delay,
mapTo,
startWith,
takeUntil,
share,
filter,
endWith,
} from 'rxjs/operators'

import { ConsoleLogger, createWebSocketConnection } from '@sourcegraph/vscode-ws-jsonrpc'
import gql from 'tagged-template-noop'
import { Settings } from './settings'
import { documentSelector } from '@sourcegraph/basic-code-intel/lib/handler'

// If we can rid ourselves of file:// URIs, this type won't be necessary and we
// can use lspext.Xreference directly.
Expand Down Expand Up @@ -546,36 +525,10 @@ function positionParams(doc: sourcegraph.TextDocument, pos: sourcegraph.Position
}
}

/**
* Emits from `fallback` after `delayMilliseconds`. Useful for falling back to
* basic-code-intel while the language server is running.
*/
function withFallback<T>({
main,
fallback,
delayMilliseconds,
}: {
main: ObservableInput<T>
fallback: ObservableInput<T>
delayMilliseconds: number
}): Observable<T> {
return from(main)
// return race(
// of(null).pipe(switchMap(() => from(main))),
// of(null).pipe(
// delay(delayMilliseconds),
// switchMap(() => from(fallback))
// )
// )
}

/**
* Uses WebSockets to communicate with a language server.
*/
export async function activateUsingWebSockets(
ctx: sourcegraph.ExtensionContext,
basicCodeIntelHandler: basicCodeIntel.Handler
): Promise<void> {
export async function activateUsingWebSockets(ctx: sourcegraph.ExtensionContext): Promise<void> {
const accessToken = await getOrTryToCreateAccessToken()
const settings: BehaviorSubject<Settings> = new BehaviorSubject<Settings>({})
ctx.subscriptions.add(
Expand Down Expand Up @@ -626,55 +579,38 @@ export async function activateUsingWebSockets(

ctx.subscriptions.add(
sourcegraph.languages.registerHoverProvider([{ pattern: '*.go' }], {
provideHover: (doc: sourcegraph.TextDocument, pos: sourcegraph.Position) =>
withFallback({
main: sendDocPositionRequest({ doc, pos, ty: lsp.HoverRequest.type, useCache: true }).then(
convert.hover
),
fallback: basicCodeIntelHandler.hover(doc, pos),
delayMilliseconds: 500,
}),
provideHover: async (doc: sourcegraph.TextDocument, pos: sourcegraph.Position) => {
const response = await sendDocPositionRequest({ doc, pos, ty: lsp.HoverRequest.type, useCache: true })
return convert.hover(response)
},
})
)

ctx.subscriptions.add(
sourcegraph.languages.registerDefinitionProvider([{ pattern: '*.go' }], {
provideDefinition: (doc: sourcegraph.TextDocument, pos: sourcegraph.Position) =>
withFallback({
main: sendDocPositionRequest({
doc,
pos,
ty: new lsp.RequestType<any, any, any, void>('textDocument/xdefinition') as any,
useCache: true,
}).then(response => convert.xdefinition({ currentDocURI: doc.uri, xdefinition: response })),
fallback: basicCodeIntelHandler.definition(doc, pos),
delayMilliseconds: 500,
}),
provideDefinition: async (doc: sourcegraph.TextDocument, pos: sourcegraph.Position) => {
const response = await sendDocPositionRequest({
doc,
pos,
ty: new lsp.RequestType<any, any, any, void>('textDocument/xdefinition') as any,
useCache: true,
})
return convert.xdefinition({ currentDocURI: doc.uri, xdefinition: response })
},
})
)

ctx.subscriptions.add(
sourcegraph.languages.registerReferenceProvider([{ pattern: '*.go' }], {
provideReferences: (doc: sourcegraph.TextDocument, pos: sourcegraph.Position) =>
withFallback({
main: sendDocPositionRequest({
doc,
pos,
ty: lsp.ReferencesRequest.type,
useCache: true,
}).then(response => ({
kind: 'main',
result: convert.references({ currentDocURI: doc.uri, references: response }),
})),
fallback: basicCodeIntelHandler.references(doc, pos).then(result => ({ kind: 'fallback', result })),
delayMilliseconds: 2000,
}).pipe(
// Indicate in the UI that the results are imprecise
tap(({ kind }) => {
sourcegraph.internal.updateContext({ isImprecise: kind === 'fallback' })
}),
map(({ result }) => result)
),
provideReferences: async (doc: sourcegraph.TextDocument, pos: sourcegraph.Position) => {
const response = await sendDocPositionRequest({
doc,
pos,
ty: lsp.ReferencesRequest.type,
useCache: true,
})
return convert.references({ currentDocURI: doc.uri, references: response })
},
})
)

Expand Down Expand Up @@ -760,61 +696,45 @@ function pathname(url: string): string {
return pathname
}

const basicCodeIntelHandlerArgs: basicCodeIntel.HandlerArgs = {
sourcegraph,
languageID: 'go',
fileExts: ['go'],
filterDefinitions: ({ repo, filePath, fileContent, results }) => {
const currentFileImportedPaths = fileContent
.split('\n')
.map(line => {
// Matches the import at index 3
const match = /^(import |\t)(\w+ |\. )?"(.*)"$/.exec(line)
return match ? match[3] : undefined
})
.filter((x): x is string => Boolean(x))

const currentFileImportPath = repo + '/' + path.dirname(filePath)

const filteredResults = results.filter(result => {
const resultImportPath = result.repo + '/' + path.dirname(result.file)
return [...currentFileImportedPaths, currentFileImportPath].some(i => resultImportPath === i)
})

return filteredResults.length === 0 ? results : filteredResults
},
commentStyle: {
lineRegex: /\/\/\s?/,
},
}

// No-op for Sourcegraph versions prior to 3.0.
const DUMMY_CTX = { subscriptions: { add: (_unsubscribable: any) => void 0 } }

export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void {
async function afterActivate(): Promise<void> {
const basicCodeIntelHandler = new basicCodeIntel.Handler(basicCodeIntelHandlerArgs)
const address = sourcegraph.configuration.get<Settings>().get('go.serverUrl')
if (address) {
await activateUsingWebSockets(ctx, basicCodeIntelHandler)
await activateUsingWebSockets(ctx)
} else {
sourcegraph.internal.updateContext({ isImprecise: true })
activateBasicCodeIntel({
sourcegraph,
languageID: 'go',
fileExts: ['go'],
filterDefinitions: ({ repo, filePath, pos, fileContent, results }) => {
const currentFileImportedPaths = fileContent
.split('\n')
.map(line => {
// Matches the import at index 3
const match = /^(import |\t)(\w+ |\. )?"(.*)"$/.exec(line)
return match ? match[3] : undefined
})
.filter((x): x is string => Boolean(x))

const currentFileImportPath = repo + '/' + path.dirname(filePath)

const filteredResults = results.filter(result => {
const resultImportPath = result.repo + '/' + path.dirname(result.file)
return (
currentFileImportedPaths.some(i => resultImportPath.includes(i)) ||
resultImportPath === currentFileImportPath
)
})

ctx.subscriptions.add(
sourcegraph.languages.registerHoverProvider(documentSelector(basicCodeIntelHandler.fileExts), {
provideHover: (doc, pos) => basicCodeIntelHandler.hover(doc, pos),
})
)
ctx.subscriptions.add(
sourcegraph.languages.registerDefinitionProvider(documentSelector(basicCodeIntelHandler.fileExts), {
provideDefinition: (doc, pos) => basicCodeIntelHandler.definition(doc, pos),
})
)
ctx.subscriptions.add(
sourcegraph.languages.registerReferenceProvider(documentSelector(basicCodeIntelHandler.fileExts), {
provideReferences: (doc, pos) => basicCodeIntelHandler.references(doc, pos),
})
)
return filteredResults.length === 0 ? results : filteredResults
},
commentStyle: {
lineRegex: /\/\/\s?/,
},
})(ctx)
}
}
setTimeout(afterActivate, 100)
Expand Down
13 changes: 4 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,10 @@
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.7.0.tgz#9a06f4f137ee84d7df0460c1fdb1135ffa6c50fd"
integrity sha512-ONhaKPIufzzrlNbqtWFFd+jlnemX6lJAgq9ZeiZtS7I1PIf/la7CW4m83rTXRnVnsMbW2k56pGYu7AUFJD9Pow==

"@sourcegraph/basic-code-intel@6.0.14":
version "6.0.14"
resolved "https://registry.yarnpkg.com/@sourcegraph/basic-code-intel/-/basic-code-intel-6.0.14.tgz#898216e8966d1b902c5e49199a2a33d255ed9de4"
integrity sha512-SekYreQAgpmeE4oNWh+Dke6ChXwudLyr+5qU79DX6lSQY1UQLg55T5cmwSbcIBLKwQzvyyUJ7YLvtQhXOn7QwA==
"@sourcegraph/basic-code-intel@6.0.15":
version "6.0.15"
resolved "https://registry.yarnpkg.com/@sourcegraph/basic-code-intel/-/basic-code-intel-6.0.15.tgz#b7c0baa894d89244fc5155a08839543cdbcca4ea"
integrity sha512-oMNbyAar7Sy4gnX5hLXQZhmLFy19zFtKndkJ4BlLCvRsmuJozMgWT32sUnhpDrco32NJQf6Irgl7a7PFX1Gb5w==
dependencies:
lodash "^4.17.11"
rxjs "^6.3.3"
Expand Down Expand Up @@ -3833,11 +3833,6 @@ path-browserify@0.0.0:
resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-0.0.0.tgz#a0b870729aae214005b7d5032ec2cbbb0fb4451a"
integrity sha1-oLhwcpquIUAFt9UDLsLLuw+0RRo=

path-browserify@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-1.0.0.tgz#40702a97af46ae00b0ea6fa8998c0b03c0af160d"
integrity sha512-Hkavx/nY4/plImrZPHRk2CL9vpOymZLgEbMNX1U0bjcBL7QN9wODxyx0yaMZURSQaUtSEvDrfAvxa9oPb0at9g==

path-dirname@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/path-dirname/-/path-dirname-1.0.2.tgz#cc33d24d525e099a5388c0336c6e32b9160609e0"
Expand Down