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

Prioritize current project import suggestions #1782

Merged
merged 5 commits into from
Jul 18, 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
2 changes: 1 addition & 1 deletion src/goPackages.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -264,7 +266,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 @@ -351,10 +353,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 @@ -368,12 +375,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