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

Commit

Permalink
Initial work on adding an import folder to workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
Shannon Wynter committed Jun 20, 2018
1 parent 47464f0 commit 77212b3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@
"title": "Go: Add Import",
"description": "Add an import declaration"
},
{
"command": "go.import.workspace",
"title": "Go: Add import to workspace"
},
{
"command": "go.tools.install",
"title": "Go: Install/Update Tools",
Expand Down Expand Up @@ -1184,4 +1188,4 @@
]
}
}
}
}
31 changes: 30 additions & 1 deletion src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
'use strict';

import vscode = require('vscode');
import { parseFilePrelude } from './util';
import path = require('path');
import fs = require('fs');
import { parseFilePrelude, getCurrentGoPath, getImportPath, getWorkspaceFolderPath } from './util';
import { documentSymbols } from './goOutline';
import { promptForMissingTool } from './goInstallTools';
import { getImportablePackages } from './goPackages';
import { fixDriveCasingInWindows, getCurrentGoWorkspaceFromGOPATH } from './goPath';

const missingToolMsg = 'Missing tool: ';

Expand Down Expand Up @@ -53,6 +56,17 @@ function askUserForImport(): Thenable<string> {
});
}

function askUserWhichImport(): Thenable<string | void> {
return getImports(vscode.window.activeTextEditor.document).then(packages => {
return vscode.window.showQuickPick(packages);
}, err => {
if (typeof err === 'string' && err.startsWith(missingToolMsg)) {
promptForMissingTool(err.substr(missingToolMsg.length));
}
});
}


export function getTextEditForAddImport(arg: string): vscode.TextEdit[] {
// Import name wasn't provided
if (arg === undefined) {
Expand Down Expand Up @@ -104,3 +118,18 @@ export function addImport(arg: string) {
}
});
}

export function addImportToWorkspace(arg: string) {
let p = arg ? Promise.resolve(arg) : askUserWhichImport();
p.then(imp => {
if (typeof imp === 'string') {
let filePath = fixDriveCasingInWindows(vscode.window.activeTextEditor.document.fileName);
let fileDirPath = path.dirname(filePath);
let currentWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), fileDirPath);
let globalPackagePath = path.join(currentWorkspace, imp)
if (fs.existsSync(globalPackagePath)) {
vscode.workspace.updateWorkspaceFolders(vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders.length : 0, null, { uri: vscode.Uri.file(globalPackagePath) })
}
}
});
}
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.import.workspace', (arg: string) => {
return addImportToWorkspace(typeof arg === 'string' ? arg : null);
}))

ctx.subscriptions.push(vscode.commands.registerCommand('go.tools.install', () => {
installAllTools();
}));
Expand Down

0 comments on commit 77212b3

Please sign in to comment.