Refactored code to make "WhiteSpace" into "Whitespace"

This commit is contained in:
pi1024e
2019-07-20 18:01:09 -04:00
parent 73c6419c66
commit 85d4895587
4 changed files with 27 additions and 31 deletions

View File

@@ -144,27 +144,23 @@ const isDir = (p: string) => {
};
function pathToReplaceRange(valueBeforeCursor: string, fullValue: string, fullValueRange: Range) {
let replaceRange: Range;
const lastIndexOfSlash = valueBeforeCursor.lastIndexOf('/');
if (lastIndexOfSlash === -1) {
replaceRange = fullValueRange;
} else {
// For cases where cursor is in the middle of attribute value, like <script src="./s|rc/test.js">
// Find the last slash before cursor, and calculate the start of replace range from there
const valueAfterLastSlash = fullValue.slice(lastIndexOfSlash + 1);
const startPos = shiftPosition(fullValueRange.end, -valueAfterLastSlash.length);
// If whitespace exists, replace until it
const whiteSpaceIndex = valueAfterLastSlash.indexOf(' ');
let endPos;
if (whiteSpaceIndex !== -1) {
endPos = shiftPosition(startPos, whiteSpaceIndex);
} else {
endPos = fullValueRange.end;
}
replaceRange = Range.create(startPos, endPos);
return fullValueRange;
}
return replaceRange;
// For cases where cursor is in the middle of attribute value, like <script src="./s|rc/test.js">
// Find the last slash before cursor, and calculate the start of replace range from there
const valueAfterLastSlash = fullValue.slice(lastIndexOfSlash + 1);
const startPos = shiftPosition(fullValueRange.end, -valueAfterLastSlash.length);
// If whitespace exists, replace until it
const whitespaceIndex = valueAfterLastSlash.indexOf(' ');
if (whitespaceIndex === -1) {
return Range.create(startPos, shiftPosition(startPos, whitespaceIndex));
}
return Range.create(startPos, fullValueRange.end);
}
function pathToSuggestion(p: string, replaceRange: Range): CompletionItem {
@@ -180,13 +176,14 @@ function pathToSuggestion(p: string, replaceRange: Range): CompletionItem {
command: 'editor.action.triggerSuggest'
}
};
} else {
return {
label: escapePath(p),
kind: CompletionItemKind.File,
textEdit: TextEdit.replace(replaceRange, escapePath(p))
};
}
return {
label: escapePath(p),
kind: CompletionItemKind.File,
textEdit: TextEdit.replace(replaceRange, escapePath(p))
};
}
// Escape https://www.w3.org/TR/CSS1/#url