From 94d914d8801bfc169a132cb3c6b663d6bde27147 Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Mon, 4 Mar 2019 16:47:04 -0800 Subject: [PATCH 1/8] wip on lsp-client (must yarn link @sourcegraph/lsp-client with changes from https://github.com/sourcegraph/lsp-client/pull/11 --- package.json | 1 + src/lang-go.ts | 109 ++++++++++++++++++++++++++++++++++--------------- yarn.lock | 35 ++++++++++++++-- 3 files changed, 109 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 868c13a..8cf4631 100644 --- a/package.json +++ b/package.json @@ -153,6 +153,7 @@ }, "dependencies": { "@sourcegraph/basic-code-intel": "6.0.16", + "@sourcegraph/lsp-client": "^2.0.0-beta.1", "@sourcegraph/vscode-ws-jsonrpc": "0.0.3-fork", "prettier": "^1.16.4", "rxjs": "^6.3.3", diff --git a/src/lang-go.ts b/src/lang-go.ts index 33d02b7..ffe4931 100644 --- a/src/lang-go.ts +++ b/src/lang-go.ts @@ -1,3 +1,4 @@ +import * as lspClient from '@sourcegraph/lsp-client' import '@babel/polyfill' import { activateBasicCodeIntel, registerFeedbackButton } from '@sourcegraph/basic-code-intel' @@ -26,6 +27,7 @@ import { import { ConsoleLogger, createWebSocketConnection } from '@sourcegraph/vscode-ws-jsonrpc' import gql from 'tagged-template-noop' import { Settings } from './settings' +import { TextDocument } from 'sourcegraph' // If we can rid ourselves of file:// URIs, this type won't be necessary and we // can use lspext.Xreference directly. @@ -739,11 +741,43 @@ const DUMMY_CTX = { subscriptions: { add: (_unsubscribable: any) => void 0 } } export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void { async function afterActivate(): Promise { - const address = sourcegraph.configuration.get().get('go.serverUrl') - if (address) { - ctx.subscriptions.add(registerFeedbackButton({ languageID: 'go', sourcegraph, isPrecise: true })) + if (Math.random() < 0) { + const address = sourcegraph.configuration.get().get('go.serverUrl') + if (address) { + ctx.subscriptions.add(registerFeedbackButton({ languageID: 'go', sourcegraph, isPrecise: true })) + await activateUsingWebSockets(ctx) + } else { + 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 + ) + }) - await activateUsingWebSockets(ctx) + return filteredResults.length === 0 ? results : filteredResults + }, + commentStyle: { + lineRegex: /\/\/\s?/, + }, + })(ctx) + } } else { ctx.subscriptions.add( registerFeedbackButton({ @@ -753,36 +787,45 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void { }) ) - 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 - ) + const token = await getOrTryToCreateAccessToken() + if (!token) { + // console.log('No token') + } else { + // tslint:disable-next-line: no-empty + const noop = () => {} + + const serverURL = sourcegraph.configuration.get().value['go.serverUrl'] + if (serverURL) { + const client = await lspClient.register({ + sourcegraph, + transport: lspClient.webSocketTransport({ + serverUrl: serverURL, + logger: { + error: noop, + warn: noop, + info: noop, + log: noop, + }, + }), + documentSelector: [{ language: 'go' }], + // clientToServerURI: uri => ..., // optional + // serverToClientURI: uri => ..., // optional + initializationOptions: (url: URL) => { + url.hash = '' + return { + zipURL: constructZipURL({ + repoName: pathname(url.href).replace(/^\/+/, ''), + revision: url.search.substr(1), + token, + }), + } + }, }) - - return filteredResults.length === 0 ? results : filteredResults - }, - commentStyle: { - lineRegex: /\/\/\s?/, - }, - })(ctx) + ctx.subscriptions.add(client) + } else { + // console.log('No go.serverUrl set') + } + } } } setTimeout(afterActivate, 100) diff --git a/yarn.lock b/yarn.lock index 22da999..c2c7855 100644 --- a/yarn.lock +++ b/yarn.lock @@ -714,6 +714,20 @@ resolved "https://registry.yarnpkg.com/@sourcegraph/prettierrc/-/prettierrc-3.0.0.tgz#716b64b287565f98f539f76bea206912ab4d03b3" integrity sha512-ziLujsC3IESHGYdtiJ7yVjte9i7mr6jKWTYtCVqgOWeY1Er0/xsQ/sdZIyK61tD282wvd7VjS4pk5RoGEN7biw== +"@sourcegraph/lsp-client@^2.0.0-beta.1": + version "2.0.0-beta.1" + resolved "https://registry.yarnpkg.com/@sourcegraph/lsp-client/-/lsp-client-2.0.0-beta.1.tgz#d231d8caba53a1ff70de20f7b442c2ea439d3b37" + integrity sha512-hho0weMoCxKk96hYkj4re7B6JxY+NccfUm2pgv0+YUKBDWRijqO8sqlgOsN8tl+H1s7UjvE7xw//cENI1N+NGA== + dependencies: + "@sourcegraph/vscode-ws-jsonrpc" "^0.0.3-fork" + lodash "^4.17.11" + rxjs "^6.4.0" + sourcegraph "^22.0.0" + type-zoo "^3.2.1" + uuid "^3.3.2" + vscode-languageserver-protocol "^3.14.1" + vscode-languageserver-types "^3.14.0" + "@sourcegraph/tsconfig@^4.0.0": version "4.0.0" resolved "https://registry.yarnpkg.com/@sourcegraph/tsconfig/-/tsconfig-4.0.0.tgz#dd2a406f90760bb789fd89fde4bd0a8d681f2767" @@ -728,7 +742,7 @@ tslint-config-prettier "^1.6.0" tslint-react "^3.2.0" -"@sourcegraph/vscode-ws-jsonrpc@0.0.3-fork": +"@sourcegraph/vscode-ws-jsonrpc@0.0.3-fork", "@sourcegraph/vscode-ws-jsonrpc@^0.0.3-fork": version "0.0.3-fork" resolved "https://registry.yarnpkg.com/@sourcegraph/vscode-ws-jsonrpc/-/vscode-ws-jsonrpc-0.0.3-fork.tgz#83728a14616ef0587298e7849d960b61853a9954" integrity sha512-EJLq/ni66glk3xYyOZtUIEbjTCw8kMI6RvO0YQtPd+4um2+aTSM1LfN4NrsiVrRkG7EG/U2OkFlKqT8mGo6w4Q== @@ -4834,7 +4848,7 @@ rxjs-tslint-rules@^4.12.0: tslib "^1.8.0" tsutils "^3.0.0" -rxjs@^6.3.3: +rxjs@^6.3.3, rxjs@^6.4.0: version "6.4.0" resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.4.0.tgz#f3bb0fe7bda7fb69deac0c16f17b50b0b8790504" integrity sha512-Z9Yfa11F6B9Sg/BK9MnqnQ+aQYicPLtilXBp2yUtDt2JRCE0h26d33EnfO3ZxoNxG0T92OUucP3Ct7cpfkdFfw== @@ -5066,6 +5080,11 @@ source-map@^0.5.0, source-map@^0.5.3, source-map@^0.5.6: dependencies: vscode-languageserver-types "^3.12.0" +sourcegraph@^22.0.0: + version "22.0.0" + resolved "https://registry.yarnpkg.com/sourcegraph/-/sourcegraph-22.0.0.tgz#b259929c31bf367f5160056698a881dc26eb36bd" + integrity sha512-cJHby0OPCM0y0BmRTKhY89D02f9koROluHMJGQGNE8q9CoIVbe1s6ctHCytz7bjY9mKtCpQMbtxYQhfIm5S4lg== + sourcegraph@^23.0.0: version "23.0.0" resolved "https://registry.yarnpkg.com/sourcegraph/-/sourcegraph-23.0.0.tgz#1fb96015af3e84cc9cc2944823f04a7e952ed565" @@ -5482,6 +5501,11 @@ type-check@~0.3.2: dependencies: prelude-ls "~1.1.2" +type-zoo@^3.2.1: + version "3.3.0" + resolved "https://registry.yarnpkg.com/type-zoo/-/type-zoo-3.3.0.tgz#d5b59393b09a48ac30380c50e2369e828817e9b3" + integrity sha512-ID2kIg4QMbujPOpphFvSl7yyk0JaN2+5m2cprhxpzJqYWHhozLbYOHCAZqq7j1PPfzLe8XrYeZfPb99v96WVRw== + typedarray@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" @@ -5599,6 +5623,11 @@ util@0.10.3, util@^0.10.3: dependencies: inherits "2.0.1" +uuid@^3.3.2: + version "3.3.2" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131" + integrity sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA== + v8-compile-cache@^2.0.0: version "2.0.2" resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.0.2.tgz#a428b28bb26790734c4fc8bc9fa106fccebf6a6c" @@ -5642,7 +5671,7 @@ vscode-languageserver-protocol@^3.14.1: vscode-jsonrpc "^4.0.0" vscode-languageserver-types "3.14.0" -vscode-languageserver-types@3.14.0, vscode-languageserver-types@^3.12.0: +vscode-languageserver-types@3.14.0, vscode-languageserver-types@^3.12.0, vscode-languageserver-types@^3.14.0: version "3.14.0" resolved "https://registry.yarnpkg.com/vscode-languageserver-types/-/vscode-languageserver-types-3.14.0.tgz#d3b5952246d30e5241592b6dde8280e03942e743" integrity sha512-lTmS6AlAlMHOvPQemVwo3CezxBp0sNB95KNPkqp3Nxd5VFEnuG1ByM0zlRWos0zjO3ZWtkvhal0COgiV1xIA4A== From dfe29694f2e7341d3c5e6ea5d2abf39c1a554c6f Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Tue, 2 Apr 2019 15:15:02 -0700 Subject: [PATCH 2/8] . --- src/lang-go.ts | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/lang-go.ts b/src/lang-go.ts index ffe4931..612cef4 100644 --- a/src/lang-go.ts +++ b/src/lang-go.ts @@ -741,7 +741,7 @@ const DUMMY_CTX = { subscriptions: { add: (_unsubscribable: any) => void 0 } } export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void { async function afterActivate(): Promise { - if (Math.random() < 0) { + if (!sourcegraph.configuration.get().get('lspclient')) { const address = sourcegraph.configuration.get().get('go.serverUrl') if (address) { ctx.subscriptions.add(registerFeedbackButton({ languageID: 'go', sourcegraph, isPrecise: true })) @@ -808,16 +808,31 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void { }, }), documentSelector: [{ language: 'go' }], - // clientToServerURI: uri => ..., // optional - // serverToClientURI: uri => ..., // optional - initializationOptions: (url: URL) => { - url.hash = '' + clientToServerURI: (uri: URL) => new URL(`file:///${uri.hash.slice(1)}`), + serverToClientURI: (uri, currentRootURI) => { + if (!currentRootURI) { + return uri + } + + if (/^file:\/\/\//.test(uri.href)) { + // The definition is in a file in the same repo + const docURL = new URL(currentRootURI.href) + docURL.hash = uri.href.slice('file:///'.length) + return docURL + } + return uri + }, + additionalInitializationOptions: (rootURI: URL) => { + const originalRootURI = rootURI.href + rootURI.hash = '' return { + originalRootURI, zipURL: constructZipURL({ - repoName: pathname(url.href).replace(/^\/+/, ''), - revision: url.search.substr(1), + repoName: pathname(rootURI.href).replace(/^\/+/, ''), + revision: rootURI.search.substr(1), token, }), + zipURLTemplate: zipURLTemplate(token), } }, }) From 54b03584b1791da98bdb0a9d4e7c5275a903557e Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Tue, 14 May 2019 10:53:18 -0700 Subject: [PATCH 3/8] . --- src/lang-go.ts | 112 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 37 deletions(-) diff --git a/src/lang-go.ts b/src/lang-go.ts index 612cef4..6a6dfa7 100644 --- a/src/lang-go.ts +++ b/src/lang-go.ts @@ -565,6 +565,43 @@ function positionParams(doc: sourcegraph.TextDocument, pos: sourcegraph.Position } } +/** + * Automatically registers/deregisters a provider based on the given predicate of the settings. + */ +function registerWhile({ + register, + settings, + settingsPredicate, +}: { + register: () => sourcegraph.Unsubscribable + settings: Observable + settingsPredicate: (settings: Settings) => boolean +}): sourcegraph.Unsubscribable { + let registration: sourcegraph.Unsubscribable | undefined + return from(settings) + .pipe( + map(settingsPredicate), + distinctUntilChanged(), + map(enabled => { + if (enabled) { + registration = register() + } else { + if (registration) { + registration.unsubscribe() + registration = undefined + } + } + }), + finalize(() => { + if (registration) { + registration.unsubscribe() + registration = undefined + } + }) + ) + .subscribe() +} + /** * Uses WebSockets to communicate with a language server. */ @@ -654,41 +691,6 @@ export async function activateUsingWebSockets(ctx: sourcegraph.ExtensionContext) }) ) - /** - * Automatically registers/deregisters a provider based on the given predicate of the settings. - */ - function registerWhile({ - register, - settingsPredicate, - }: { - register: () => sourcegraph.Unsubscribable - settingsPredicate: (settings: Settings) => boolean - }): sourcegraph.Unsubscribable { - let registration: sourcegraph.Unsubscribable | undefined - return from(settings) - .pipe( - map(settingsPredicate), - distinctUntilChanged(), - map(enabled => { - if (enabled) { - registration = register() - } else { - if (registration) { - registration.unsubscribe() - registration = undefined - } - } - }), - finalize(() => { - if (registration) { - registration.unsubscribe() - registration = undefined - } - }) - ) - .subscribe() - } - ctx.subscriptions.add( registerWhile({ register: () => @@ -703,6 +705,7 @@ export async function activateUsingWebSockets(ctx: sourcegraph.ExtensionContext) map(response => convert.xreferences({ references: response })) ), }), + settings, settingsPredicate: settings => Boolean(settings['go.showExternalReferences']), }) ) @@ -742,6 +745,7 @@ const DUMMY_CTX = { subscriptions: { add: (_unsubscribable: any) => void 0 } } export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void { async function afterActivate(): Promise { if (!sourcegraph.configuration.get().get('lspclient')) { + console.log('old') const address = sourcegraph.configuration.get().get('go.serverUrl') if (address) { ctx.subscriptions.add(registerFeedbackButton({ languageID: 'go', sourcegraph, isPrecise: true })) @@ -779,6 +783,8 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void { })(ctx) } } else { + console.log('lspclient') + ctx.subscriptions.add( registerFeedbackButton({ languageID: 'go', @@ -808,7 +814,10 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void { }, }), documentSelector: [{ language: 'go' }], - clientToServerURI: (uri: URL) => new URL(`file:///${uri.hash.slice(1)}`), + clientToServerURI: (uri: URL) => { + return uri + }, + // clientToServerURI: (uri: URL) => new URL(`file:///${uri.hash.slice(1)}`), serverToClientURI: (uri, currentRootURI) => { if (!currentRootURI) { return uri @@ -826,7 +835,8 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void { const originalRootURI = rootURI.href rootURI.hash = '' return { - originalRootURI, + // originalRootURI, + // TODO drop zipURL, keep zipURLTemplate (different PR) zipURL: constructZipURL({ repoName: pathname(rootURI.href).replace(/^\/+/, ''), revision: rootURI.search.substr(1), @@ -837,6 +847,34 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void { }, }) ctx.subscriptions.add(client) + const settings: BehaviorSubject = new BehaviorSubject({}) + ctx.subscriptions.add( + sourcegraph.configuration.subscribe(() => { + settings.next(sourcegraph.configuration.get().value) + }) + ) + + ctx.subscriptions.add( + registerWhile({ + register: () => + sourcegraph.languages.registerReferenceProvider([{ pattern: '*.go' }], { + provideReferences: (doc: sourcegraph.TextDocument, pos: sourcegraph.Position) => + xrefs({ + doc, + pos, + sendRequest: ({ rootURI, requestType, request, useCache }) => + client.withConnection(rootURI, conn => + conn.sendRequest(requestType, request) + ), + }).pipe( + scan((acc: XRef[], curr: XRef) => [...acc, curr], [] as XRef[]), + map(response => convert.xreferences({ references: response })) + ), + }), + settings, + settingsPredicate: settings => Boolean(settings['go.showExternalReferences']), + }) + ) } else { // console.log('No go.serverUrl set') } From 09e9351f402d8cc2319addcdf2be0c5a81dad71f Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Mon, 27 May 2019 20:21:46 -0700 Subject: [PATCH 4/8] . --- src/lang-go.ts | 32 ++------------------------------ 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/src/lang-go.ts b/src/lang-go.ts index 6a6dfa7..f527966 100644 --- a/src/lang-go.ts +++ b/src/lang-go.ts @@ -814,36 +814,8 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void { }, }), documentSelector: [{ language: 'go' }], - clientToServerURI: (uri: URL) => { - return uri - }, - // clientToServerURI: (uri: URL) => new URL(`file:///${uri.hash.slice(1)}`), - serverToClientURI: (uri, currentRootURI) => { - if (!currentRootURI) { - return uri - } - - if (/^file:\/\/\//.test(uri.href)) { - // The definition is in a file in the same repo - const docURL = new URL(currentRootURI.href) - docURL.hash = uri.href.slice('file:///'.length) - return docURL - } - return uri - }, - additionalInitializationOptions: (rootURI: URL) => { - const originalRootURI = rootURI.href - rootURI.hash = '' - return { - // originalRootURI, - // TODO drop zipURL, keep zipURLTemplate (different PR) - zipURL: constructZipURL({ - repoName: pathname(rootURI.href).replace(/^\/+/, ''), - revision: rootURI.search.substr(1), - token, - }), - zipURLTemplate: zipURLTemplate(token), - } + initializationOptions: { + zipURLTemplate: zipURLTemplate(token), }, }) ctx.subscriptions.add(client) From 1d15a8ec7828307134fc672c74e77047db11bcad Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Tue, 28 May 2019 13:05:59 -0700 Subject: [PATCH 5/8] new lsp-client --- package.json | 2 +- yarn.lock | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 8cf4631..97b2692 100644 --- a/package.json +++ b/package.json @@ -153,7 +153,7 @@ }, "dependencies": { "@sourcegraph/basic-code-intel": "6.0.16", - "@sourcegraph/lsp-client": "^2.0.0-beta.1", + "@sourcegraph/lsp-client": "1.1.0-beta.2", "@sourcegraph/vscode-ws-jsonrpc": "0.0.3-fork", "prettier": "^1.16.4", "rxjs": "^6.3.3", diff --git a/yarn.lock b/yarn.lock index c2c7855..ed784f7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -709,25 +709,25 @@ rxjs "^6.3.3" sourcegraph "^23.0.0" -"@sourcegraph/prettierrc@^3.0.0": - version "3.0.0" - resolved "https://registry.yarnpkg.com/@sourcegraph/prettierrc/-/prettierrc-3.0.0.tgz#716b64b287565f98f539f76bea206912ab4d03b3" - integrity sha512-ziLujsC3IESHGYdtiJ7yVjte9i7mr6jKWTYtCVqgOWeY1Er0/xsQ/sdZIyK61tD282wvd7VjS4pk5RoGEN7biw== - -"@sourcegraph/lsp-client@^2.0.0-beta.1": - version "2.0.0-beta.1" - resolved "https://registry.yarnpkg.com/@sourcegraph/lsp-client/-/lsp-client-2.0.0-beta.1.tgz#d231d8caba53a1ff70de20f7b442c2ea439d3b37" - integrity sha512-hho0weMoCxKk96hYkj4re7B6JxY+NccfUm2pgv0+YUKBDWRijqO8sqlgOsN8tl+H1s7UjvE7xw//cENI1N+NGA== +"@sourcegraph/lsp-client@1.1.0-beta.2": + version "1.1.0-beta.2" + resolved "https://registry.yarnpkg.com/@sourcegraph/lsp-client/-/lsp-client-1.1.0-beta.2.tgz#69e82aef44f74e6ab49f68b8cf9fb508531483d9" + integrity sha512-fdPb8zFV7WEXVBjaOajcrTkPlbT2BZD6JRqH2M5/qPGQRD2AOn8pV7tb7YHKpQv5or4iZFaFgTrcXQZEEOOQqw== dependencies: "@sourcegraph/vscode-ws-jsonrpc" "^0.0.3-fork" lodash "^4.17.11" rxjs "^6.4.0" - sourcegraph "^22.0.0" + sourcegraph "^23.0.1" type-zoo "^3.2.1" uuid "^3.3.2" vscode-languageserver-protocol "^3.14.1" vscode-languageserver-types "^3.14.0" +"@sourcegraph/prettierrc@^3.0.0": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@sourcegraph/prettierrc/-/prettierrc-3.0.0.tgz#716b64b287565f98f539f76bea206912ab4d03b3" + integrity sha512-ziLujsC3IESHGYdtiJ7yVjte9i7mr6jKWTYtCVqgOWeY1Er0/xsQ/sdZIyK61tD282wvd7VjS4pk5RoGEN7biw== + "@sourcegraph/tsconfig@^4.0.0": version "4.0.0" resolved "https://registry.yarnpkg.com/@sourcegraph/tsconfig/-/tsconfig-4.0.0.tgz#dd2a406f90760bb789fd89fde4bd0a8d681f2767" @@ -5080,16 +5080,16 @@ source-map@^0.5.0, source-map@^0.5.3, source-map@^0.5.6: dependencies: vscode-languageserver-types "^3.12.0" -sourcegraph@^22.0.0: - version "22.0.0" - resolved "https://registry.yarnpkg.com/sourcegraph/-/sourcegraph-22.0.0.tgz#b259929c31bf367f5160056698a881dc26eb36bd" - integrity sha512-cJHby0OPCM0y0BmRTKhY89D02f9koROluHMJGQGNE8q9CoIVbe1s6ctHCytz7bjY9mKtCpQMbtxYQhfIm5S4lg== - sourcegraph@^23.0.0: version "23.0.0" resolved "https://registry.yarnpkg.com/sourcegraph/-/sourcegraph-23.0.0.tgz#1fb96015af3e84cc9cc2944823f04a7e952ed565" integrity sha512-hQALHrTt+AK5ZAqUAUNhxO7ClSv/xyTjMQPBb+hQykKJrMaHTk+CnJNHs7dRfXYKnbnZeisQRjaxKGX8UecU0Q== +sourcegraph@^23.0.1: + version "23.0.1" + resolved "https://registry.yarnpkg.com/sourcegraph/-/sourcegraph-23.0.1.tgz#715fcf4129a6d94bc3bfd2740d9c706ae6357ffe" + integrity sha512-4We7zqhOagOVxNFdS6/xT/Crhb0Arw/9ytGBu8JuHfjo5yjMtcUYt22kZyu2TaPHXwyPW9muUi1eKSFA6Qg4lw== + spawn-command@^0.0.2-1: version "0.0.2-1" resolved "https://registry.yarnpkg.com/spawn-command/-/spawn-command-0.0.2-1.tgz#62f5e9466981c1b796dc5929937e11c9c6921bd0" From 5134002ff978107f6d3c705c5547439d061de586 Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Tue, 28 May 2019 15:16:13 -0700 Subject: [PATCH 6/8] newer lsp-client --- package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 97b2692..cae3ca1 100644 --- a/package.json +++ b/package.json @@ -153,7 +153,7 @@ }, "dependencies": { "@sourcegraph/basic-code-intel": "6.0.16", - "@sourcegraph/lsp-client": "1.1.0-beta.2", + "@sourcegraph/lsp-client": "2.0.0-beta.2", "@sourcegraph/vscode-ws-jsonrpc": "0.0.3-fork", "prettier": "^1.16.4", "rxjs": "^6.3.3", diff --git a/yarn.lock b/yarn.lock index ed784f7..0579677 100644 --- a/yarn.lock +++ b/yarn.lock @@ -709,10 +709,10 @@ rxjs "^6.3.3" sourcegraph "^23.0.0" -"@sourcegraph/lsp-client@1.1.0-beta.2": - version "1.1.0-beta.2" - resolved "https://registry.yarnpkg.com/@sourcegraph/lsp-client/-/lsp-client-1.1.0-beta.2.tgz#69e82aef44f74e6ab49f68b8cf9fb508531483d9" - integrity sha512-fdPb8zFV7WEXVBjaOajcrTkPlbT2BZD6JRqH2M5/qPGQRD2AOn8pV7tb7YHKpQv5or4iZFaFgTrcXQZEEOOQqw== +"@sourcegraph/lsp-client@2.0.0-beta.2": + version "2.0.0-beta.2" + resolved "https://registry.yarnpkg.com/@sourcegraph/lsp-client/-/lsp-client-2.0.0-beta.2.tgz#755363aed4bfc0d1860b5c82023e647c737c335e" + integrity sha512-u9kBLoU3cXEoWJkFmF53R2PjV5+WVFGcYbZr+jvxXaDm3igtZGmMcAr6KUX1e9t9S1foNnPJa/ukoXfTgHyZ7g== dependencies: "@sourcegraph/vscode-ws-jsonrpc" "^0.0.3-fork" lodash "^4.17.11" From 16a380351cb71fad25974413d8d4035b3a2b33f0 Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Tue, 28 May 2019 18:30:19 -0700 Subject: [PATCH 7/8] only use file:// URIs for older go-langserver versions --- src/lang-go.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/lang-go.ts b/src/lang-go.ts index f527966..310c337 100644 --- a/src/lang-go.ts +++ b/src/lang-go.ts @@ -483,7 +483,12 @@ function xrefs({ const definitions = (await sendRequest({ rootURI: rootURIFromDoc(doc), requestType: new lsp.RequestType('textDocument/xdefinition') as any, - request: positionParams(doc, pos), + request: { + textDocument: { + uri: doc.uri, + }, + position: pos, + }, useCache: true, })) as lspext.Xdefinition[] | null if (!definitions) { @@ -553,18 +558,6 @@ function xrefs({ ) } -function positionParams(doc: sourcegraph.TextDocument, pos: sourcegraph.Position): lsp.TextDocumentPositionParams { - return { - textDocument: { - uri: `file:///${new URL(doc.uri).hash.slice(1)}`, - }, - position: { - line: pos.line, - character: pos.character, - }, - } -} - /** * Automatically registers/deregisters a provider based on the given predicate of the settings. */ @@ -650,7 +643,12 @@ export async function activateUsingWebSockets(ctx: sourcegraph.ExtensionContext) sendRequest({ rootURI: rootURIFromDoc(doc), requestType: ty, - request: positionParams(doc, pos), + request: { + textDocument: { + uri: `file:///${new URL(doc.uri).hash.slice(1)}`, + }, + position: pos, + }, useCache, }) From 0a8f4a1abaa2862701327f69205787da2c6fa93a Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Wed, 29 May 2019 13:45:43 -0700 Subject: [PATCH 8/8] Restructure --- src/lang-go.ts | 207 +++++++++++++++++++++++-------------------------- 1 file changed, 99 insertions(+), 108 deletions(-) diff --git a/src/lang-go.ts b/src/lang-go.ts index 310c337..b8971fa 100644 --- a/src/lang-go.ts +++ b/src/lang-go.ts @@ -28,6 +28,7 @@ import { ConsoleLogger, createWebSocketConnection } from '@sourcegraph/vscode-ws import gql from 'tagged-template-noop' import { Settings } from './settings' import { TextDocument } from 'sourcegraph' +import { NoopLogger } from '@sourcegraph/lsp-client/dist/logging' // If we can rid ourselves of file:// URIs, this type won't be necessary and we // can use lspext.Xreference directly. @@ -598,7 +599,9 @@ function registerWhile({ /** * Uses WebSockets to communicate with a language server. */ -export async function activateUsingWebSockets(ctx: sourcegraph.ExtensionContext): Promise { +export async function activatePreciseDeprecated(ctx: sourcegraph.ExtensionContext): Promise { + ctx.subscriptions.add(registerFeedbackButton({ languageID: 'go', sourcegraph, isPrecise: true })) + const accessToken = await getOrTryToCreateAccessToken() const settings: BehaviorSubject = new BehaviorSubject({}) ctx.subscriptions.add( @@ -730,6 +733,94 @@ export async function activateUsingWebSockets(ctx: sourcegraph.ExtensionContext) ctx.subscriptions.add(panelView) } +function activateImprecise(ctx: sourcegraph.ExtensionContext): void { + ctx.subscriptions.add( + registerFeedbackButton({ + languageID: 'go', + sourcegraph, + isPrecise: false, + }) + ) + + 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 + ) + }) + + return filteredResults.length === 0 ? results : filteredResults + }, + commentStyle: { + lineRegex: /\/\/\s?/, + }, + })(ctx) +} + +async function activatePreciseUsingLspClient(ctx: sourcegraph.ExtensionContext): Promise { + ctx.subscriptions.add(registerFeedbackButton({ languageID: 'go', sourcegraph, isPrecise: true })) + + const token = await getOrTryToCreateAccessToken() + + const serverURL = sourcegraph.configuration.get().value['go.serverUrl'] + const client = await lspClient.register({ + sourcegraph, + transport: lspClient.webSocketTransport({ + serverUrl: serverURL!, + logger: new NoopLogger(), + }), + documentSelector: [{ language: 'go' }], + initializationOptions: { + zipURLTemplate: zipURLTemplate(token), + }, + }) + ctx.subscriptions.add(client) + + const settings: BehaviorSubject = new BehaviorSubject({}) + ctx.subscriptions.add( + sourcegraph.configuration.subscribe(() => { + settings.next(sourcegraph.configuration.get().value) + }) + ) + + ctx.subscriptions.add( + registerWhile({ + register: () => + sourcegraph.languages.registerReferenceProvider([{ pattern: '*.go' }], { + provideReferences: (doc: sourcegraph.TextDocument, pos: sourcegraph.Position) => + xrefs({ + doc, + pos, + sendRequest: ({ rootURI, requestType, request, useCache }) => + client.withConnection(rootURI, conn => conn.sendRequest(requestType, request)), + }).pipe( + scan((acc: XRef[], curr: XRef) => [...acc, curr], [] as XRef[]), + map(response => convert.xreferences({ references: response })) + ), + }), + settings, + settingsPredicate: settings => Boolean(settings['go.showExternalReferences']), + }) + ) +} + function pathname(url: string): string { let pathname = url pathname = pathname.slice('git://'.length) @@ -741,115 +832,15 @@ function pathname(url: string): string { const DUMMY_CTX = { subscriptions: { add: (_unsubscribable: any) => void 0 } } export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void { - async function afterActivate(): Promise { - if (!sourcegraph.configuration.get().get('lspclient')) { - console.log('old') - const address = sourcegraph.configuration.get().get('go.serverUrl') - if (address) { - ctx.subscriptions.add(registerFeedbackButton({ languageID: 'go', sourcegraph, isPrecise: true })) - await activateUsingWebSockets(ctx) + setTimeout(async function afterActivate(): Promise { + if (sourcegraph.configuration.get().get('go.serverUrl')) { + if (sourcegraph.configuration.get().get('lspclient')) { + await activatePreciseUsingLspClient(ctx) } else { - 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 - ) - }) - - return filteredResults.length === 0 ? results : filteredResults - }, - commentStyle: { - lineRegex: /\/\/\s?/, - }, - })(ctx) + await activatePreciseDeprecated(ctx) } } else { - console.log('lspclient') - - ctx.subscriptions.add( - registerFeedbackButton({ - languageID: 'go', - sourcegraph, - isPrecise: false, - }) - ) - - const token = await getOrTryToCreateAccessToken() - if (!token) { - // console.log('No token') - } else { - // tslint:disable-next-line: no-empty - const noop = () => {} - - const serverURL = sourcegraph.configuration.get().value['go.serverUrl'] - if (serverURL) { - const client = await lspClient.register({ - sourcegraph, - transport: lspClient.webSocketTransport({ - serverUrl: serverURL, - logger: { - error: noop, - warn: noop, - info: noop, - log: noop, - }, - }), - documentSelector: [{ language: 'go' }], - initializationOptions: { - zipURLTemplate: zipURLTemplate(token), - }, - }) - ctx.subscriptions.add(client) - const settings: BehaviorSubject = new BehaviorSubject({}) - ctx.subscriptions.add( - sourcegraph.configuration.subscribe(() => { - settings.next(sourcegraph.configuration.get().value) - }) - ) - - ctx.subscriptions.add( - registerWhile({ - register: () => - sourcegraph.languages.registerReferenceProvider([{ pattern: '*.go' }], { - provideReferences: (doc: sourcegraph.TextDocument, pos: sourcegraph.Position) => - xrefs({ - doc, - pos, - sendRequest: ({ rootURI, requestType, request, useCache }) => - client.withConnection(rootURI, conn => - conn.sendRequest(requestType, request) - ), - }).pipe( - scan((acc: XRef[], curr: XRef) => [...acc, curr], [] as XRef[]), - map(response => convert.xreferences({ references: response })) - ), - }), - settings, - settingsPredicate: settings => Boolean(settings['go.showExternalReferences']), - }) - ) - } else { - // console.log('No go.serverUrl set') - } - } + activateImprecise(ctx) } - } - setTimeout(afterActivate, 100) + }, 100) }