Proposal to implement collapsing (#500)
* With this commit: 1. Add new setting "go.blockImportsOnly" 2. Add parsing of package name for single import lines 3. When the user adds an import manually and there is already one or more single imports, collapse it all into a block if go.blockImportsOnly is true. * WTC: 1. Fix linter issues * Remove unused import * With this commit: 1. Back out package name parsing 2. Remove setting from package.json (always use block imports) 3. Switch up how block import is created to preserve comments and special imports * Post rebase cleanup * Add tests
This commit is contained in:
Родитель
acecbf2a95
Коммит
3790d5f08a
|
@ -53,7 +53,7 @@ function askUserForImport(): Thenable<string> {
|
|||
});
|
||||
}
|
||||
|
||||
export function getTextEditForAddImport(arg: string): vscode.TextEdit {
|
||||
export function getTextEditForAddImport(arg: string): vscode.TextEdit[] {
|
||||
// Import name wasn't provided
|
||||
if (arg === undefined) {
|
||||
return null;
|
||||
|
@ -66,31 +66,41 @@ export function getTextEditForAddImport(arg: string): vscode.TextEdit {
|
|||
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`);
|
||||
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');
|
||||
return [vscode.TextEdit.insert(new vscode.Position(lastImportSection.start + 1, 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');
|
||||
// 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(imports[0].start, 0), 'import (\n\t"' + arg + '"\n'));
|
||||
imports.forEach(element => {
|
||||
const currentLine = vscode.window.activeTextEditor.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(imports[imports.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');
|
||||
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;
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function addImport(arg: string) {
|
||||
let p = arg ? Promise.resolve(arg) : askUserForImport();
|
||||
p.then(imp => {
|
||||
let edit = getTextEditForAddImport(imp);
|
||||
if (edit) {
|
||||
vscode.window.activeTextEditor.edit(editBuilder => {
|
||||
editBuilder.insert(edit.range.start, edit.newText);
|
||||
});
|
||||
let edits = getTextEditForAddImport(imp);
|
||||
if (edits && edits.length > 0) {
|
||||
const edit = new vscode.WorkspaceEdit();
|
||||
edit.set(vscode.window.activeTextEditor.document.uri, edits);
|
||||
vscode.workspace.applyEdit(edit);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -104,7 +104,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
|
|||
// 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.additionalTextEdits = [getTextEditForAddImport(pkgPath)];
|
||||
item.additionalTextEdits = getTextEditForAddImport(pkgPath);
|
||||
});
|
||||
resolve(newsuggestions);
|
||||
});
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package hello
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func one() {
|
||||
fmt.Print(math.Max(1, 2))
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
package hello
|
|
@ -0,0 +1,8 @@
|
|||
package hello
|
||||
|
||||
import "fmt"
|
||||
import . "math" // comment
|
||||
|
||||
func two() {
|
||||
fmt.Print(Max(1, 2))
|
||||
}
|
|
@ -19,7 +19,7 @@ import jsDiff = require('diff');
|
|||
import { testCurrentFile } from '../src/goTest';
|
||||
import { getBinPath, getGoVersion, isVendorSupported } from '../src/util';
|
||||
import { documentSymbols } from '../src/goOutline';
|
||||
import { listPackages } from '../src/goImport';
|
||||
import { listPackages, getTextEditForAddImport } from '../src/goImport';
|
||||
import { generateTestCurrentFile, generateTestCurrentPackage, generateTestCurrentFunction } from '../src/goGenerateTests';
|
||||
import { getAllPackages } from '../src/goPackages';
|
||||
import { getImportPath } from '../src/util';
|
||||
|
@ -58,6 +58,9 @@ suite('Go Extension Tests', () => {
|
|||
fs.copySync(path.join(fixtureSourcePath, 'linterTest', 'linter_2.go'), path.join(testPath, 'linterTest', 'linter_2.go'));
|
||||
fs.copySync(path.join(fixtureSourcePath, 'buildTags', 'hello.go'), path.join(fixturePath, 'buildTags', 'hello.go'));
|
||||
fs.copySync(path.join(fixtureSourcePath, 'completions', 'unimportedPkgs.go'), path.join(fixturePath, 'completions', 'unimportedPkgs.go'));
|
||||
fs.copySync(path.join(fixtureSourcePath, 'importTest', 'noimports.go'), path.join(fixturePath, 'importTest', 'noimports.go'));
|
||||
fs.copySync(path.join(fixtureSourcePath, 'importTest', 'groupImports.go'), path.join(fixturePath, 'importTest', 'groupImports.go'));
|
||||
fs.copySync(path.join(fixtureSourcePath, 'importTest', 'singleImports.go'), path.join(fixturePath, 'importTest', 'singleImports.go'));
|
||||
});
|
||||
|
||||
suiteTeardown(() => {
|
||||
|
@ -905,4 +908,53 @@ It returns the number of bytes written and any write error encountered.
|
|||
Promise.all([checkWithTags, checkWithMultipleTags, checkWithoutTags]).then(() => done(), done);
|
||||
|
||||
});
|
||||
|
||||
test('Add imports when no imports', (done) => {
|
||||
let uri = vscode.Uri.file(path.join(fixturePath, 'importTest', 'noimports.go'));
|
||||
vscode.workspace.openTextDocument(uri).then(document => {
|
||||
return vscode.window.showTextDocument(document).then(editor => {
|
||||
const expectedText = document.getText() + '\n' + 'import (\n\t"bytes"\n)\n';
|
||||
const edits = getTextEditForAddImport('bytes');
|
||||
const edit = new vscode.WorkspaceEdit();
|
||||
edit.set(document.uri, edits);
|
||||
return vscode.workspace.applyEdit(edit).then(() => {
|
||||
assert.equal(vscode.window.activeTextEditor.document.getText(), expectedText);
|
||||
return Promise.resolve();
|
||||
});
|
||||
});
|
||||
}).then(() => done(), done);
|
||||
});
|
||||
|
||||
test('Add imports to an import block', (done) => {
|
||||
let uri = vscode.Uri.file(path.join(fixturePath, 'importTest', 'groupImports.go'));
|
||||
vscode.workspace.openTextDocument(uri).then(document => {
|
||||
return vscode.window.showTextDocument(document).then(editor => {
|
||||
const expectedText = document.getText().replace('\t"fmt"\n\t"math"', '\t"bytes"\n\t"fmt"\n\t"math"');
|
||||
const edits = getTextEditForAddImport('bytes');
|
||||
const edit = new vscode.WorkspaceEdit();
|
||||
edit.set(document.uri, edits);
|
||||
return vscode.workspace.applyEdit(edit).then(() => {
|
||||
assert.equal(vscode.window.activeTextEditor.document.getText(), expectedText);
|
||||
return Promise.resolve();
|
||||
});
|
||||
});
|
||||
}).then(() => done(), done);
|
||||
});
|
||||
|
||||
test('Add imports and collapse single imports to an import block', (done) => {
|
||||
let uri = vscode.Uri.file(path.join(fixturePath, 'importTest', 'singleImports.go'));
|
||||
vscode.workspace.openTextDocument(uri).then(document => {
|
||||
return vscode.window.showTextDocument(document).then(editor => {
|
||||
const expectedText = document.getText().replace('import "fmt"\nimport . "math" // comment', 'import (\n\t"bytes"\n\t"fmt"\n\t. "math" // comment\n)');
|
||||
const edits = getTextEditForAddImport('bytes');
|
||||
const edit = new vscode.WorkspaceEdit();
|
||||
edit.set(document.uri, edits);
|
||||
return vscode.workspace.applyEdit(edit).then(() => {
|
||||
assert.equal(vscode.window.activeTextEditor.document.getText(), expectedText);
|
||||
return Promise.resolve();
|
||||
});
|
||||
});
|
||||
}).then(() => done(), done);
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче