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

Add Import to Workspace #1745

Merged
merged 4 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@
"title": "Go: Add Import",
"description": "Add an import declaration"
},
{
"command": "go.add.package.workspace",
"title": "Go: Add Package to Workspace",
"description": "Add a package from the imports list to the workspace."
},
{
"command": "go.tools.install",
"title": "Go: Install/Update Tools",
Expand Down
58 changes: 57 additions & 1 deletion src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
'use strict';

import vscode = require('vscode');
import { parseFilePrelude } from './util';
import cp = require('child_process');
import { parseFilePrelude, getImportPath, getBinPath, getToolsEnvVars } from './util';
import { documentSymbols } from './goOutline';
import { promptForMissingTool } from './goInstallTools';
import { getImportablePackages } from './goPackages';
Expand Down Expand Up @@ -104,3 +105,58 @@ export function addImport(arg: string) {
}
});
}

export function addImportToWorkspace() {
const editor = vscode.window.activeTextEditor;
const selection = editor.selection;

let importPath = '';
if (!selection.isEmpty) {
// Attempt to load a partial import path based on currently selected text
let selectedText = editor.document.getText(selection).trim();
if (selectedText.length > 0) {
if (!selectedText.startsWith('"')) {
selectedText = '"' + selectedText;
}
if (!selectedText.endsWith('"')) {
selectedText = selectedText + '"';
}
importPath = getImportPath(selectedText);
}
}

if (importPath === '') {
// Failing that use the current line
let selectedText = editor.document.lineAt(selection.active.line).text;
importPath = getImportPath(selectedText);
}

if (importPath === '') {
vscode.window.showErrorMessage('No import path to add');
return;
}

const goRuntimePath = getBinPath('go');
const env = getToolsEnvVars();

cp.execFile(goRuntimePath, ['list', '-f', '{{.Dir}}', importPath], { env }, (err, stdout, stderr) => {
if (!stdout) {
return;
}

let dirs = stdout.split('\n');
if (dirs.length === 0) {
return;
}

const importPathUri = vscode.Uri.file(dirs[0]);

const existingWorkspaceFolder = vscode.workspace.getWorkspaceFolder(importPathUri);
if (existingWorkspaceFolder !== undefined) {
vscode.window.showInformationMessage('Already available under ' + existingWorkspaceFolder.name);
return;
}

vscode.workspace.updateWorkspaceFolders(vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders.length : 0, null, { uri: importPathUri });
});
}
6 changes: 5 additions & 1 deletion src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { initGoCover } from './goCover';
import { testAtCursor, testCurrentPackage, testCurrentFile, testPrevious, testWorkspace } from './goTest';
import { showTestOutput, cancelRunningTests } from './testUtils';
import * as goGenerateTests from './goGenerateTests';
import { addImport } from './goImport';
import { addImport, addImportToWorkspace } from './goImport';
import { installAllTools, checkLanguageServer } from './goInstallTools';
import { isGoPathSet, getBinPath, sendTelemetryEvent, getExtensionCommands, getGoVersion, getCurrentGoPath, getToolsGopath, handleDiagnosticErrors, disposeTelemetryReporter, getToolsEnvVars } from './util';
import { LanguageClient, RevealOutputChannelOn, FormattingOptions, ProvideDocumentFormattingEditsSignature, ProvideCompletionItemsSignature } from 'vscode-languageclient';
Expand Down Expand Up @@ -274,6 +274,10 @@ export function activate(ctx: vscode.ExtensionContext): void {
return addImport(typeof arg === 'string' ? arg : null);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.add.package.workspace', () => {
addImportToWorkspace();
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.tools.install', () => {
installAllTools();
}));
Expand Down
3 changes: 2 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,4 +811,5 @@ export function makeMemoizedByteOffsetConverter(buffer: Buffer): (byteOffset: nu
memo.insert(byteOffset, nearest.value + charDelta);
return nearest.value + charDelta;
};
}
}