|
| 1 | +import * as lspClient from '@sourcegraph/lsp-client' |
1 | 2 | import '@babel/polyfill'
|
2 | 3 |
|
3 | 4 | import { activateBasicCodeIntel } from '@sourcegraph/basic-code-intel'
|
@@ -26,6 +27,7 @@ import {
|
26 | 27 | import { ConsoleLogger, createWebSocketConnection } from '@sourcegraph/vscode-ws-jsonrpc'
|
27 | 28 | import gql from 'tagged-template-noop'
|
28 | 29 | import { Settings } from './settings'
|
| 30 | +import { TextDocument } from 'sourcegraph' |
29 | 31 |
|
30 | 32 | // If we can rid ourselves of file:// URIs, this type won't be necessary and we
|
31 | 33 | // can use lspext.Xreference directly.
|
@@ -701,40 +703,82 @@ const DUMMY_CTX = { subscriptions: { add: (_unsubscribable: any) => void 0 } }
|
701 | 703 |
|
702 | 704 | export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void {
|
703 | 705 | async function afterActivate(): Promise<void> {
|
704 |
| - const address = sourcegraph.configuration.get<Settings>().get('go.serverUrl') |
705 |
| - if (address) { |
706 |
| - await activateUsingWebSockets(ctx) |
707 |
| - } else { |
708 |
| - activateBasicCodeIntel({ |
709 |
| - sourcegraph, |
710 |
| - languageID: 'go', |
711 |
| - fileExts: ['go'], |
712 |
| - filterDefinitions: ({ repo, filePath, pos, fileContent, results }) => { |
713 |
| - const currentFileImportedPaths = fileContent |
714 |
| - .split('\n') |
715 |
| - .map(line => { |
716 |
| - // Matches the import at index 3 |
717 |
| - const match = /^(import |\t)(\w+ |\. )?"(.*)"$/.exec(line) |
718 |
| - return match ? match[3] : undefined |
| 706 | + if (Math.random() < 0) { |
| 707 | + const address = sourcegraph.configuration.get<Settings>().get('go.serverUrl') |
| 708 | + if (address) { |
| 709 | + await activateUsingWebSockets(ctx) |
| 710 | + } else { |
| 711 | + activateBasicCodeIntel({ |
| 712 | + sourcegraph, |
| 713 | + languageID: 'go', |
| 714 | + fileExts: ['go'], |
| 715 | + filterDefinitions: ({ repo, filePath, pos, fileContent, results }) => { |
| 716 | + const currentFileImportedPaths = fileContent |
| 717 | + .split('\n') |
| 718 | + .map(line => { |
| 719 | + // Matches the import at index 3 |
| 720 | + const match = /^(import |\t)(\w+ |\. )?"(.*)"$/.exec(line) |
| 721 | + return match ? match[3] : undefined |
| 722 | + }) |
| 723 | + .filter((x): x is string => Boolean(x)) |
| 724 | + |
| 725 | + const currentFileImportPath = repo + '/' + path.dirname(filePath) |
| 726 | + |
| 727 | + const filteredResults = results.filter(result => { |
| 728 | + const resultImportPath = result.repo + '/' + path.dirname(result.file) |
| 729 | + return ( |
| 730 | + currentFileImportedPaths.some(i => resultImportPath.includes(i)) || |
| 731 | + resultImportPath === currentFileImportPath |
| 732 | + ) |
719 | 733 | })
|
720 |
| - .filter((x): x is string => Boolean(x)) |
721 |
| - |
722 |
| - const currentFileImportPath = repo + '/' + path.dirname(filePath) |
723 | 734 |
|
724 |
| - const filteredResults = results.filter(result => { |
725 |
| - const resultImportPath = result.repo + '/' + path.dirname(result.file) |
726 |
| - return ( |
727 |
| - currentFileImportedPaths.some(i => resultImportPath.includes(i)) || |
728 |
| - resultImportPath === currentFileImportPath |
729 |
| - ) |
| 735 | + return filteredResults.length === 0 ? results : filteredResults |
| 736 | + }, |
| 737 | + commentStyle: { |
| 738 | + lineRegex: /\/\/\s?/, |
| 739 | + }, |
| 740 | + })(ctx) |
| 741 | + } |
| 742 | + } else { |
| 743 | + const token = await getOrTryToCreateAccessToken() |
| 744 | + if (!token) { |
| 745 | + // console.log('No token') |
| 746 | + } else { |
| 747 | + // tslint:disable-next-line: no-empty |
| 748 | + const noop = () => {} |
| 749 | + |
| 750 | + const serverURL = sourcegraph.configuration.get<Settings>().value['go.serverUrl'] |
| 751 | + if (serverURL) { |
| 752 | + const client = await lspClient.register({ |
| 753 | + sourcegraph, |
| 754 | + transport: lspClient.webSocketTransport({ |
| 755 | + serverUrl: serverURL, |
| 756 | + logger: { |
| 757 | + error: noop, |
| 758 | + warn: noop, |
| 759 | + info: noop, |
| 760 | + log: noop, |
| 761 | + }, |
| 762 | + }), |
| 763 | + documentSelector: [{ language: 'go' }], |
| 764 | + // clientToServerURI: uri => ..., // optional |
| 765 | + // serverToClientURI: uri => ..., // optional |
| 766 | + initializationOptions: (url: URL) => { |
| 767 | + url.hash = '' |
| 768 | + return { |
| 769 | + zipURL: constructZipURL({ |
| 770 | + repoName: pathname(url.href).replace(/^\/+/, ''), |
| 771 | + revision: url.search.substr(1), |
| 772 | + token, |
| 773 | + }), |
| 774 | + } |
| 775 | + }, |
730 | 776 | })
|
731 |
| - |
732 |
| - return filteredResults.length === 0 ? results : filteredResults |
733 |
| - }, |
734 |
| - commentStyle: { |
735 |
| - lineRegex: /\/\/\s?/, |
736 |
| - }, |
737 |
| - })(ctx) |
| 777 | + ctx.subscriptions.add(client) |
| 778 | + } else { |
| 779 | + // console.log('No go.serverUrl set') |
| 780 | + } |
| 781 | + } |
738 | 782 | }
|
739 | 783 | }
|
740 | 784 | setTimeout(afterActivate, 100)
|
|
0 commit comments