Skip to content

Commit

Permalink
Updates the logic for getting the user path.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihai-vlc committed Jun 2, 2017
1 parent 6eaf929 commit 43abbe8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "path-autocomplete",
"displayName": "Path Autocomplete",
"description": "Provides path completion for visual studio code.",
"version": "1.4.1",
"version": "1.5.0",
"publisher": "ionutvmi",
"icon": "icon.png",
"repository": {
Expand Down
34 changes: 12 additions & 22 deletions src/features/PathAutocompleteProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ export class PathAutocomplete implements vs.CompletionItemProvider {
*/
getFolderPath(fileName: string, currentLine: string, currentPosition: number): string {

var insertedPath = this.getInsertedPath(currentLine, currentPosition);
var mappingResult = this.applyMapping(insertedPath);
var userPath = this.getUserPath(currentLine, currentPosition);
var mappingResult = this.applyMapping(userPath);
var insertedPath = mappingResult.insertedPath;
var currentDir = mappingResult.currentDir || this.getCurrentDirectory(fileName, insertedPath);

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

getInsertedPath(currentLine: string, currentPosition: number): string {
/**
* Retrieves the path inserted by the user. This is taken based on the last quote or last white space character.
*
* @param currentLine The current line of the cursor.
* @param currentPosition The current position of the cursor.
*/
getUserPath(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++) {
Expand All @@ -169,26 +171,14 @@ export class PathAutocomplete implements vs.CompletionItemProvider {
}

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

var pathBegin;
if (insideSingleQuote || insideDoubleQuote || insideBacktickQuote) {
pathBegin = lastQuote + 1;
} else {
pathBegin = lastWhiteSpace + 1;
}
var startPosition = (lastQuote != -1) ? lastQuote : lastWhiteSpace;

return currentLine.substring(pathBegin, currentPosition);
return currentLine.substring(startPosition + 1, currentPosition);
}

/**
Expand Down

0 comments on commit 43abbe8

Please sign in to comment.