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

Commit

Permalink
Prioritize current project import suggestions (#1782)
Browse files Browse the repository at this point in the history
* Prioritize current project import suggestions

* fix project import prioritization to account for multi root projects

* goSuggest: sort standard library before root pkgs

* Refactoring

* Linter
  • Loading branch information
marwan-at-work authored and ramya-rao-a committed Jul 18, 2018
1 parent 18a313a commit 5f43e98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/goPackages.ts
Expand Up @@ -188,7 +188,7 @@ export function getImportablePackages(filePath: string, useCache: boolean = fals
if (!foundPkgRootDir) {
// try to guess package root dir
let vendorIndex = pkgPath.indexOf('/vendor/');
if (vendorIndex !== -1 ) {
if (vendorIndex !== -1) {
foundPkgRootDir = path.join(currentWorkspace, pkgPath.substring(0, vendorIndex).replace('/', path.sep));
pkgRootDirs.set(fileDirPath, foundPkgRootDir);
}
Expand Down
21 changes: 15 additions & 6 deletions src/goSuggest.ts
Expand Up @@ -5,9 +5,11 @@

'use strict';

import path = require('path');
import vscode = require('vscode');
import cp = require('child_process');
import { getBinPath, getParametersAndReturnType, parseFilePrelude, isPositionInString, goKeywords, getToolsEnvVars, guessPackageNameFromFile, goBuiltinTypes, byteOffsetAt } from './util';
import { getCurrentGoPath, getBinPath, getParametersAndReturnType, parseFilePrelude, isPositionInString, goKeywords, getToolsEnvVars, guessPackageNameFromFile, goBuiltinTypes, byteOffsetAt } from './util';
import { getCurrentGoWorkspaceFromGOPATH } from './goPath';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
import { getTextEditForAddImport } from './goImport';
import { getImportablePackages } from './goPackages';
Expand Down Expand Up @@ -267,7 +269,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
}

// Add importable packages matching currentword to suggestions
let importablePkgs = includeUnimportedPkgs ? this.getMatchingPackages(currentWord, suggestionSet) : [];
let importablePkgs = includeUnimportedPkgs ? this.getMatchingPackages(document, currentWord, suggestionSet) : [];
suggestions = suggestions.concat(importablePkgs);

// 'Smart Snippet' for package clause
Expand Down Expand Up @@ -354,10 +356,15 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
}

// Return importable packages that match given word as Completion Items
private getMatchingPackages(word: string, suggestionSet: Set<string>): vscode.CompletionItem[] {
private getMatchingPackages(document: vscode.TextDocument, word: string, suggestionSet: Set<string>): vscode.CompletionItem[] {
if (!word) return [];
let completionItems = [];

const cwd = path.dirname(document.fileName);
const goWorkSpace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), cwd);
const workSpaceFolder = vscode.workspace.getWorkspaceFolder(document.uri);
const currentPkgRootPath = (workSpaceFolder ? workSpaceFolder.uri.path : cwd).slice(goWorkSpace.length + 1);

let completionItems = [];
this.pkgsList.forEach((pkgName: string, pkgPath: string) => {
if (pkgName.startsWith(word) && !suggestionSet.has(pkgName)) {

Expand All @@ -371,12 +378,14 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
arguments: [pkgPath]
};
item.kind = vscode.CompletionItemKind.Module;
// Add same sortText to the unimported packages so that they appear after the suggestions from gocode

// Unimported packages should appear after the suggestions from gocode
const isStandardPackage = !item.detail.includes('.');
item.sortText = isStandardPackage ? 'za' : 'zb';
item.sortText = isStandardPackage ? 'za' : pkgPath.startsWith(currentPkgRootPath) ? 'zb' : 'zc';
completionItems.push(item);
}
});

return completionItems;
}

Expand Down

0 comments on commit 5f43e98

Please sign in to comment.