Skip to content
Open
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
8 changes: 3 additions & 5 deletions Extension/src/LanguageServer/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import * as which from 'which';
import { logAndReturn, returns } from '../Utility/Async/returns';
import { escapePathForSquiggles } from '../Utility/Text/escape';
import { escapePathForSquiggles, getTextMatchOffsets } from '../Utility/Text/escape';
import * as util from '../common';
import { isWindows } from '../constants';
import { getOutputChannelLogger } from '../logger';
Expand Down Expand Up @@ -2134,8 +2134,7 @@ export class CppProperties {
expandedPaths = result ?? [];
if (duration > 10 && configMatches) {
newSquiggleMetrics.SlowPathResolution++;
const curOffset = curText.indexOf(configMatches[0]);
const endOffset = curOffset + curPath.length;
const [curOffset, endOffset] = getTextMatchOffsets(curText, configMatches[0]);
const diagnostic: vscode.Diagnostic = new vscode.Diagnostic(
new vscode.Range(document.positionAt(curTextStartOffset + curOffset), document.positionAt(curTextStartOffset + endOffset)),
localize('resolve.path.took.too.long', "Path took {0}s to evaluate", duration),
Expand All @@ -2145,8 +2144,7 @@ export class CppProperties {
} catch (e) {
expandedPaths = [];
if (configMatches) {
const curOffset = curText.indexOf(configMatches[0]);
const endOffset = curOffset + curPath.length;
const [curOffset, endOffset] = getTextMatchOffsets(curText, configMatches[0]);
const diagnostic: vscode.Diagnostic = new vscode.Diagnostic(
new vscode.Range(document.positionAt(curTextStartOffset + curOffset), document.positionAt(curTextStartOffset + endOffset)),
localize('resolve.path.failed', "Failed to resolve path {0}. Error: {1}", curPath, (e as Error).message),
Expand Down
5 changes: 5 additions & 0 deletions Extension/src/Utility/Text/escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ export function escapePathForSquiggles(s: string): string {
return s.replace(/[-"\/\\^$*+?.()|[\]{}]/g, (character: string): string =>
character === '"' ? '\\\\"' : `\\${character}`);
}

export function getTextMatchOffsets(text: string, match: string): [number, number] {
const startOffset: number = text.indexOf(match);
return [startOffset, startOffset + match.length];
}
30 changes: 28 additions & 2 deletions Extension/test/unit/escape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* ------------------------------------------------------------------------------------------ */

import { describe, it } from 'mocha';
import { doesNotMatch, match, strictEqual } from 'node:assert';
import { escapePathForSquiggles } from '../../src/Utility/Text/escape';
import { deepStrictEqual, doesNotMatch, match, ok, strictEqual } from 'node:assert';
import { escapePathForSquiggles, getTextMatchOffsets } from '../../src/Utility/Text/escape';

describe('Text escaping', () => {
it('escapes paths for matching their JSON spelling', () => {
Expand All @@ -29,4 +29,30 @@ describe('Text escaping', () => {
doesNotMatch(String.raw`C:\sdk\[headers]+(x)?.h\say\"hello\"and\"goodbye`, pattern);
doesNotMatch(String.raw`C:\\sdk\\headers+(x)?.h\\say\"hello\"and\"goodbye`, pattern);
});

it('uses the full source match for a non-first semicolon-delimited path', () => {
const parsedPath: string = 'second';
const sourceMatch: string = '"first;second;third"';
const text: string = `"includePath": [${sourceMatch}]`;
const pattern: RegExp = new RegExp(`"[^"]*?(?<="|;)${escapePathForSquiggles(parsedPath)}(?="|;).*?"`, 'g');
const matches: string[] | null = text.match(pattern);

ok(matches);
strictEqual(matches?.[0], sourceMatch);
const startOffset: number = text.indexOf(sourceMatch);
deepStrictEqual(getTextMatchOffsets(text, matches[0]), [startOffset, startOffset + sourceMatch.length]);
});

it('uses the JSON source length when it differs from the parsed path', () => {
const parsedPath: string = 'folder"quoted"';
const sourceMatch: string = String.raw`"folder\"quoted\""`;
const text: string = `"includePath": [${sourceMatch}]`;
const pattern: RegExp = new RegExp(`"[^"]*?(?<="|;)${escapePathForSquiggles(parsedPath)}(?="|;).*?"`, 'g');
const matches: string[] | null = text.match(pattern);

ok(matches);
strictEqual(matches?.[0], sourceMatch);
const startOffset: number = text.indexOf(sourceMatch);
deepStrictEqual(getTextMatchOffsets(text, matches[0]), [startOffset, startOffset + sourceMatch.length]);
});
});
Loading