Skip to content

Commit

Permalink
Merge pull request #14 from W4RH4WK/outside-string
Browse files Browse the repository at this point in the history
Add option to use path autocompletion outside strings.
  • Loading branch information
mihai-vlc authored Jun 2, 2017
2 parents 697c871 + 1a1e0f2 commit 6eaf929
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
"type": "array",
"default": [],
"description": "Custom transformations applied to the inserted text."
},
"path-autocomplete.triggerOutsideStrings": {
"type": "boolean",
"default": false,
"description": "Enables path autocompletion outside strings."
}
}
}
Expand Down
57 changes: 53 additions & 4 deletions src/features/PathAutocompleteProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const withExtension = vs.workspace.getConfiguration('path-autocomplete')['extens
const excludedItems = vs.workspace.getConfiguration('path-autocomplete')['excludedItems'];
const pathMappings = vs.workspace.getConfiguration('path-autocomplete')['pathMappings'];
const transformations = vs.workspace.getConfiguration('path-autocomplete')['transformations'];
const triggerOutsideStrings = vs.workspace.getConfiguration('path-autocomplete')['triggerOutsideStrings'];
const homeDirectory = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];

export class PathAutocomplete implements vs.CompletionItemProvider {
Expand Down Expand Up @@ -121,10 +122,8 @@ export class PathAutocomplete implements vs.CompletionItemProvider {
*/
getFolderPath(fileName: string, currentLine: string, currentPosition: number): string {

// extract the inserted text from the quote to the cursor to obtain the inserted path
var text = currentLine.substring(0, currentPosition);
var startPosition = Math.max(text.lastIndexOf('"'), text.lastIndexOf("'"), text.lastIndexOf("`"));
var mappingResult = this.applyMapping(startPosition != -1 ? text.substring(startPosition + 1) : '');
var insertedPath = this.getInsertedPath(currentLine, currentPosition);
var mappingResult = this.applyMapping(insertedPath);
var insertedPath = mappingResult.insertedPath;
var currentDir = mappingResult.currentDir || this.getCurrentDirectory(fileName, insertedPath);

Expand All @@ -146,6 +145,52 @@ export class PathAutocomplete implements vs.CompletionItemProvider {
return path.join(currentDir, insertedPath);
}

getInsertedPath(currentLine: string, currentPosition: number): string {
var lastQuote = -1;
var insideSingleQuote = false;
var insideDoubleQuote = false;
var insideBacktickQuote = false;

var lastWhiteSpace = -1;

for (var i = 0; i < currentPosition; i++) {
var c = currentLine[i];

// skip next character if escaped
if (c == "\\") {
i++;
continue;
}

// handle space
if (c == " " || c == "\t") {
lastWhiteSpace = i;
continue;
}

// handle quotes
if (c == "'") {
insideSingleQuote = !insideSingleQuote;
lastQuote = i;
} else if (c == '"') {
insideDoubleQuote = !insideDoubleQuote;
lastQuote = i;
} else if (c == "`") {
insideBacktickQuote = !insideBacktickQuote;
lastQuote = i;
}
}

var pathBegin;
if (insideSingleQuote || insideDoubleQuote || insideBacktickQuote) {
pathBegin = lastQuote + 1;
} else {
pathBegin = lastWhiteSpace + 1;
}

return currentLine.substring(pathBegin, currentPosition);
}

/**
* Searches for the node_modules folder in the parent folders of the current directory.
*
Expand Down Expand Up @@ -236,6 +281,10 @@ export class PathAutocomplete implements vs.CompletionItemProvider {
* Determine if we should provide path completion.
*/
shouldProvide(currentLine: string, position: number) {
if (triggerOutsideStrings) {
return true;
}

var quotes = {
single: 0,
double: 0,
Expand Down

0 comments on commit 6eaf929

Please sign in to comment.