Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Use additionalTextEdits so that 'useCodeSnippetsOnFunctionSuggest' fe…
Browse files Browse the repository at this point in the history
…ature continues to work
  • Loading branch information
ramya-rao-a committed Sep 29, 2016
1 parent 2ec1763 commit 85b4a05
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
59 changes: 30 additions & 29 deletions src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,37 @@ function askUserForImport(): Thenable<string> {
});
}

export function getTextEditForAddImport(arg: string): vscode.TextEdit {
// Import name wasn't provided
if (arg === undefined) {
return null;
}

let {imports, pkg} = parseFilePrelude(vscode.window.activeTextEditor.document.getText());
let multis = imports.filter(x => x.kind === 'multi');
if (multis.length > 0) {
// There is a multiple import declaration, add to the last one
let closeParenLine = multis[multis.length - 1].end;
return vscode.TextEdit.insert(new vscode.Position(closeParenLine, 0), '\t"' + arg + '"\n');
} else if (imports.length > 0) {
// There are only single import declarations, add after the last one
let lastSingleImport = imports[imports.length - 1].end;
return vscode.TextEdit.insert(new vscode.Position(lastSingleImport + 1, 0), 'import "' + arg + '"\n');
} 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 null;
}
}

export function addImport(arg: string) {
let p = arg ? Promise.resolve(arg) : askUserForImport();
p.then(imp => {
// Import name wasn't provided
if (imp === undefined) {
return null;
}

let {imports, pkg} = parseFilePrelude(vscode.window.activeTextEditor.document.getText());
let multis = imports.filter(x => x.kind === 'multi');
if (multis.length > 0) {
// There is a multiple import declaration, add to the last one
let closeParenLine = multis[multis.length - 1].end;
return vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.insert(new vscode.Position(closeParenLine, 0), '\t"' + imp + '"\n');
});
} else if (imports.length > 0) {
// There are only single import declarations, add after the last one
let lastSingleImport = imports[imports.length - 1].end;
return vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.insert(new vscode.Position(lastSingleImport + 1, 0), 'import "' + imp + '"\n');
});
} else if (pkg && pkg.start >= 0) {
// There are no import declarations, but there is a package declaration
return vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.insert(new vscode.Position(pkg.start + 1, 0), '\nimport (\n\t"' + imp + '"\n)\n');
});
} else {
// There are no imports and no package declaration - give up
return null;
}
let edit = getTextEditForAddImport(imp);
vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.insert(edit.range.start, edit.newText);
});
});
}
}
11 changes: 4 additions & 7 deletions src/goSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { dirname, basename } from 'path';
import { getBinPath } from './goPath';
import { parameters, parseFilePrelude } from './util';
import { promptForMissingTool } from './goInstallTools';
import { listPackages } from './goImport';
import { listPackages, getTextEditForAddImport } from './goImport';

function vscodeKindFromGoCodeClass(kind: string): vscode.CompletionItemKind {
switch (kind) {
Expand Down Expand Up @@ -96,13 +96,10 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
// Now that we have the package imported in the inputText, run gocode again
return this.runGoCode(filename, inputText, offset, inString, position, lineText).then(newsuggestions => {
// Since the new suggestions are due to the package that we imported,
// add a command to do the same in the actual document in the editor
// add additionalTextEdits to do the same in the actual document in the editor
// We use additionalTextEdits instead of command so that 'useCodeSnippetsOnFunctionSuggest' feature continues to work
newsuggestions.forEach(item => {
item.command = {
title: 'Import Package',
command: 'go.import.add',
arguments: [pkgPath]
};
item.additionalTextEdits = [getTextEditForAddImport(pkgPath)];
});
resolve(newsuggestions);
});
Expand Down

0 comments on commit 85b4a05

Please sign in to comment.