Skip to content

Commit

Permalink
Fix search text in virtual jars and add highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthurm1 committed Mar 16, 2022
1 parent d09f118 commit 76ee30b
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/findInFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
TreeDataProvider,
TreeItem,
TreeItemCollapsibleState,
TreeItemLabel,
TreeView,
Uri,
window,
Expand Down Expand Up @@ -162,17 +163,17 @@ async function toTopLevel(locations: Location[]): Promise<TopLevel[]> {
return await Promise.all(
Array.from(locationsByFile, async ([filePath, locations]) => {
const uri: Uri = Uri.parse(filePath);

const readData = await workspace.fs.readFile(uri);
const fileContent = Buffer.from(readData).toString("utf8");
const lines = fileContent.split(/\r?\n/);

const newPositions = locations.reduce((arr, location) => {
const line = lines[location.range.start.line];
const newPosition = new PositionInFile(location, uri, line.trimStart());
return [...arr, newPosition];
}, [] as PositionInFile[]);
return new TopLevel(newPositions, uri);
const document = await workspace.openTextDocument(uri);
if (document) {
const newPositions = locations.reduce((arr, location) => {
const line = document.lineAt(location.range.start.line);
const newPosition = new PositionInFile(location, uri, line.text);
return [...arr, newPosition];
}, [] as PositionInFile[]);
return new TopLevel(newPositions, uri);
} else {
return new TopLevel([] as PositionInFile[], uri);
}
})
);
}
Expand All @@ -196,11 +197,23 @@ class FindInFilesProvider implements TreeDataProvider<Node> {
}
case "PositionInFile": {
const start = element.location.range.start;
const end = element.location.range.end;
const line = start.line;
const startColumn = start.character;
const fileName = element.uri.fsPath.split("/").pop();
const shortDescription = fileName + ":" + (line + 1);
const shortDescription =
fileName + ":" + (line + 1) + ":" + (startColumn + 1);
const trimmedLabel = element.label;
const trimmedStartCol =
startColumn + trimmedLabel.length - element.label.length;
const trimmedEndCol =
end.character + trimmedLabel.length - element.label.length;
const highlightedLabel: TreeItemLabel = {
label: trimmedLabel,
highlights: [[trimmedStartCol, trimmedEndCol]],
};
const positionResult: TreeItem = {
label: element.label,
label: highlightedLabel,
description: shortDescription,
resourceUri: element.uri,
};
Expand Down

0 comments on commit 76ee30b

Please sign in to comment.