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

Commit

Permalink
Improve string literal detection method
Browse files Browse the repository at this point in the history
  • Loading branch information
lggomez committed Jul 2, 2018
1 parent 14067ad commit 09bca18
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/goSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,37 @@ export class GoSignatureHelpProvider implements SignatureHelpProvider {

private getStringLiteralIndexes(currentLine: string): IndexRangeArray {
// Index string literal boundaries
// This is needed to detect string literals and ignore commas within them
// This is needed to detect string literals and avoid pushing commas within them
let stringLiteralBoundaries = new IndexRangeArray();
let stringLiteralRegex = RegExp('([\`\'\"])(?:(?!(?:\\\\|\\1)).|\\\\.)*\\1', 'giu');
let m: RegExpExecArray | null = null;
do {
m = stringLiteralRegex.exec(currentLine);
if (m) {
stringLiteralBoundaries.PushIndex(m.index);
stringLiteralBoundaries.PushIndex(stringLiteralRegex.lastIndex - 1);
stringLiteralBoundaries.PushIndex(stringLiteralRegex.lastIndex);
}
} while (m);

// Check against incomplete string literals as line endings
m = RegExp('(?:.(?![\'`\"]))+$', 'giu').exec(currentLine);
if (m) {
stringLiteralBoundaries.PushIndex(m.index + 1);
// Check for incomplete literals as current line terminators
for (let char = stringLiteralBoundaries.LastIndex; char < currentLine.length; char++) {
if ((currentLine[char] === '`')
|| (currentLine[char] === '\'')
|| (currentLine[char] === '"')) {
stringLiteralBoundaries.PushIndex(char);
break;
}
}

return stringLiteralBoundaries;
}
}

/*
* Array wrapper that contains indexes treated as pairs
* An odd number of elements will consider the last
* element as the start of a new (incomplete) range
*/
class IndexRangeArray {
LastIndex: number;
Array: Array<number>;
Expand All @@ -168,6 +177,8 @@ class IndexRangeArray {
this.LastIndex = index;
}

// Traverse the index pairs contained in the array
// This assumes a forward (increasing) order
public IsWithinPairRange(index: number): boolean {
let isEven = (this.Array.length % 2) === 0;
if (index > this.LastIndex) return !isEven;
Expand Down

0 comments on commit 09bca18

Please sign in to comment.