Skip to content

Commit

Permalink
src/language/legacy: delete legacy language feature providers
Browse files Browse the repository at this point in the history
Except the legacy goFormat provider. It is still used for custom
formatter feature implementation.

For #2799

Change-Id: Ia4c04bc584110d88b728b7ee144a255645e458b6
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/535096
Reviewed-by: Suzy Mueller <[email protected]>
TryBot-Result: kokoro <[email protected]>
Commit-Queue: Hyang-Ah Hana Kim <[email protected]>
  • Loading branch information
hyangah committed Oct 25, 2023
1 parent e8b29bc commit 340f78b
Show file tree
Hide file tree
Showing 23 changed files with 15 additions and 3,507 deletions.
2 changes: 1 addition & 1 deletion src/commands/startLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const startLanguageServer: CommandFactory = (ctx, goCtx) => {
}

if (!cfg.enabled) {
const legacyService = new LegacyLanguageService(ctx, goCtx);
const legacyService = new LegacyLanguageService();
goCtx.legacyLanguageService = legacyService;
ctx.subscriptions.push(legacyService);
updateStatus(goCtx, goConfig, false);
Expand Down
10 changes: 4 additions & 6 deletions src/goDocumentSymbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ import vscode = require('vscode');
import { DocumentSymbolRequest, ExecuteCommandParams, ExecuteCommandRequest } from 'vscode-languageserver-protocol';
import { getGoConfig } from './config';
import { GoExtensionContext } from './context';
import { GoLegacyDocumentSymbolProvider } from './language/legacy/goOutline';

export function GoDocumentSymbolProvider(
goCtx: GoExtensionContext,
includeImports?: boolean
): GoplsDocumentSymbolProvider | GoLegacyDocumentSymbolProvider {
const { latestConfig } = goCtx;
if (!latestConfig?.enabled) {
return new GoLegacyDocumentSymbolProvider(includeImports);
}
): GoplsDocumentSymbolProvider {
return new GoplsDocumentSymbolProvider(goCtx, includeImports);
}

Expand All @@ -25,6 +20,9 @@ export class GoplsDocumentSymbolProvider implements vscode.DocumentSymbolProvide
constructor(private readonly goCtx: GoExtensionContext, private includeImports?: boolean) {}

public async provideDocumentSymbols(document: vscode.TextDocument): Promise<vscode.DocumentSymbol[]> {
if (!this.goCtx.languageServerIsRunning) {
return [];
}
// TODO(suzmue): consider providing an interface for providing document symbols that only requires
// the URI. Getting a TextDocument from a filename requires opening the file, which can lead to
// files being opened that were not requested by the user in order to get information that we just
Expand Down
3 changes: 3 additions & 0 deletions src/goGenerateTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ function generateTests(
async function getFunctions(goCtx: GoExtensionContext, doc: vscode.TextDocument): Promise<vscode.DocumentSymbol[]> {
const documentSymbolProvider = GoDocumentSymbolProvider(goCtx);
const symbols = await documentSymbolProvider.provideDocumentSymbols(doc);
if (!symbols || symbols.length == 0) {
return [];
}
return symbols[0].children.filter((sym) =>
[vscode.SymbolKind.Function, vscode.SymbolKind.Method].includes(sym.kind)
);
Expand Down
115 changes: 1 addition & 114 deletions src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import vscode = require('vscode');
import { ExecuteCommandRequest, ExecuteCommandParams } from 'vscode-languageserver-protocol';
import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool } from './goInstallTools';
import { documentSymbols, GoOutlineImportsOptions } from './language/legacy/goOutline';
import { getImportablePackages } from './goPackages';
import { getBinPath, getImportPath, parseFilePrelude } from './util';
import { getEnvPath, getCurrentGoRoot } from './utils/pathUtils';
Expand All @@ -21,30 +20,6 @@ import { CommandFactory } from './commands';

const missingToolMsg = 'Missing tool: ';

// listPackages returns 'importable' packages and places std packages first.
export async function listPackages(excludeImportedPkgs = false): Promise<string[]> {
const importedPkgs =
excludeImportedPkgs && vscode.window.activeTextEditor
? await getImports(vscode.window.activeTextEditor.document)
: [];
const pkgMap = vscode.window.activeTextEditor
? await getImportablePackages(vscode.window.activeTextEditor?.document.fileName, true)
: new Map();
const stdLibs: string[] = [];
const nonStdLibs: string[] = [];
pkgMap.forEach((value, key) => {
if (importedPkgs.some((imported) => imported === key)) {
return;
}
if (value.isStd) {
stdLibs.push(key);
} else {
nonStdLibs.push(key);
}
});
return [...stdLibs.sort(), ...nonStdLibs.sort()];
}

async function golist(goCtx: GoExtensionContext): Promise<string[]> {
const { languageClient, serverInfo } = goCtx;
const COMMAND = 'gopls.list_known_packages';
Expand All @@ -71,31 +46,7 @@ async function golist(goCtx: GoExtensionContext): Promise<string[]> {
}
}

// fallback to calling listPackages
return listPackages(true);
}

/**
* Returns the imported packages in the given file
*
* @param document TextDocument whose imports need to be returned
* @returns Array of imported package paths wrapped in a promise
*/
async function getImports(document: vscode.TextDocument): Promise<string[]> {
const options = {
fileName: document.fileName,
importsOption: GoOutlineImportsOptions.Only,
document
};
const symbols = await documentSymbols(options);
if (!symbols || !symbols.length) {
return [];
}
// import names will be of the form "math", so strip the quotes in the beginning and the end
const imports = symbols[0].children
.filter((x: any) => x.kind === vscode.SymbolKind.Namespace)
.map((x: any) => x.name.substr(1, x.name.length - 2));
return imports;
return [];
}

async function askUserForImport(goCtx: GoExtensionContext): Promise<string | undefined> {
Expand All @@ -108,62 +59,6 @@ async function askUserForImport(goCtx: GoExtensionContext): Promise<string | und
}
}
}

export function getTextEditForAddImport(arg: string | undefined): vscode.TextEdit[] | undefined {
// Import name wasn't provided
if (arg === undefined) {
return undefined;
}
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage('No active editor found.');
return [];
}

const { imports, pkg } = parseFilePrelude(editor.document.getText());
if (imports.some((block) => block.pkgs.some((pkgpath) => pkgpath === arg))) {
return [];
}

const multis = imports.filter((x) => x.kind === 'multi');
const minusCgo = imports.filter((x) => x.kind !== 'pseudo');

if (multis.length > 0) {
// There is a multiple import declaration, add to the last one
const lastImportSection = multis[multis.length - 1];
if (lastImportSection.end === -1) {
// For some reason there was an empty import section like `import ()`
return [vscode.TextEdit.insert(new vscode.Position(lastImportSection.start + 1, 0), `import "${arg}"\n`)];
}
// Add import at the start of the block so that goimports/goreturns can order them correctly
return [vscode.TextEdit.insert(new vscode.Position(lastImportSection.start + 1, 0), '\t"' + arg + '"\n')];
} else if (minusCgo.length > 0) {
// There are some number of single line imports, which can just be collapsed into a block import.
const edits = [];

edits.push(vscode.TextEdit.insert(new vscode.Position(minusCgo[0].start, 0), 'import (\n\t"' + arg + '"\n'));
minusCgo.forEach((element) => {
const currentLine = editor.document.lineAt(element.start).text;
const updatedLine = currentLine.replace(/^\s*import\s*/, '\t');
edits.push(
vscode.TextEdit.replace(
new vscode.Range(element.start, 0, element.start, currentLine.length),
updatedLine
)
);
});
edits.push(vscode.TextEdit.insert(new vscode.Position(minusCgo[minusCgo.length - 1].end + 1, 0), ')\n'));

return edits;
} else if (pkg && pkg.start >= 0) {
// There are no import declarations, but there is a package declaration
return [vscode.TextEdit.insert(new vscode.Position(pkg.start + 1, 0), '\nimport (\n\t"' + arg + '"\n)\n')];
} else {
// There are no imports and no package declaration - give up
return [];
}
}

export const addImport: CommandFactory = (ctx, goCtx) => (arg: { importPath: string }) => {
const { languageClient, serverInfo } = goCtx;
const editor = vscode.window.activeTextEditor;
Expand Down Expand Up @@ -201,14 +96,6 @@ export const addImport: CommandFactory = (ctx, goCtx) => (arg: { importPath: str
console.log(`error executing gopls.add_import: ${e}`);
}
}

// fallback to adding imports directly from client
const edits = getTextEditForAddImport(imp);
if (edits && edits.length > 0) {
const edit = new vscode.WorkspaceEdit();
edit.set(editor.document.uri, edits);
vscode.workspace.applyEdit(edit);
}
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/goRunTestCodelens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {

private async getCodeLensForPackage(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
const documentSymbolProvider = GoDocumentSymbolProvider(this.goCtx);
const symbols = await documentSymbolProvider.provideDocumentSymbols(document, token);
const symbols = await documentSymbolProvider.provideDocumentSymbols(document);
if (!symbols || symbols.length === 0) {
return [];
}
Expand Down
4 changes: 2 additions & 2 deletions src/goTest/explore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export class GoTestExplorer {

const ctrl = vscode.tests.createTestController('go', 'Go');
const symProvider = GoDocumentSymbolProvider(goCtx, true);
const inst = new this(goCtx, workspace, ctrl, context.workspaceState, (doc, token) =>
symProvider.provideDocumentSymbols(doc, token)
const inst = new this(goCtx, workspace, ctrl, context.workspaceState, (doc) =>
symProvider.provideDocumentSymbols(doc)
);

// Process already open editors
Expand Down
6 changes: 0 additions & 6 deletions src/goTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import path = require('path');
import semver = require('semver');
import util = require('util');
import { getFormatTool, usingCustomFormatTool } from './language/legacy/goFormat';
import { goLiveErrorsEnabled } from './language/legacy/goLiveErrors';
import { allToolsInformation } from './goToolsInformation';
import { getBinPath, GoVersion } from './util';

Expand Down Expand Up @@ -209,11 +208,6 @@ export function getConfiguredTools(
if (useLanguageServer) {
maybeAddTool('gopls');
}

if (goLiveErrorsEnabled()) {
maybeAddTool('gotype-live');
}

return tools;
}

Expand Down
54 changes: 0 additions & 54 deletions src/language/legacy/goCodeAction.ts

This file was deleted.

Loading

0 comments on commit 340f78b

Please sign in to comment.