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 17, 2022
1 parent a40aaad commit f501d53
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,7 @@ function launchMetals(
client,
findInFilesProvider,
findInFilesView,
metalsFileProvider,
outputChannel
)
);
Expand Down
60 changes: 45 additions & 15 deletions src/findInFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import {
OutputChannel,
Position,
Range,
Selection,
TextEditorRevealType,
TreeDataProvider,
TreeItem,
TreeItemCollapsibleState,
TreeItemLabel,
TreeView,
Uri,
window,
workspace,
} from "vscode";
import { LanguageClient, Location } from "vscode-languageclient/node";
import { MetalsFileProvider } from "./metalsContentProvider";

class TopLevel {
constructor(
Expand Down Expand Up @@ -75,11 +78,15 @@ export function createFindInFilesTreeView(
);
const textEditor = await window.showTextDocument(textDocument);
const range = positionInFile.location.range;
const start = new Position(range.start.line, range.start.character);
const end = new Position(range.end.line, range.end.character);
const selection = new Selection(end, start);
const vscodeRange = new Range(
new Position(range.start.line, range.start.character),
new Position(range.end.line, range.end.character)
);
textEditor.revealRange(vscodeRange, TextEditorRevealType.InCenter);
textEditor.selection = selection;
break;
}
}
Expand All @@ -94,6 +101,7 @@ export async function executeFindInFiles(
client: LanguageClient,
provider: FindInFilesProvider,
view: TreeView<unknown>,
metalsFileProvider: MetalsFileProvider,
outputChannel: OutputChannel
): Promise<void> {
try {
Expand Down Expand Up @@ -135,7 +143,7 @@ export async function executeFindInFiles(
);

commands.executeCommand("setContext", "metals.showFindInFiles", true);
const newTopLevel = await toTopLevel(locations);
const newTopLevel = await toTopLevel(locations, metalsFileProvider);

provider.update(newTopLevel);

Expand All @@ -151,7 +159,10 @@ export async function executeFindInFiles(
}
}

async function toTopLevel(locations: Location[]): Promise<TopLevel[]> {
async function toTopLevel(
locations: Location[],
metalsFileProvider: MetalsFileProvider
): Promise<TopLevel[]> {
const locationsByFile = new Map<string, Location[]>();

for (const loc of locations) {
Expand All @@ -162,17 +173,24 @@ 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);
var fileContent;
if (uri.scheme == "jar") {
fileContent = await metalsFileProvider.provideTextDocumentContent(uri);
} else {
const readData = await workspace.fs.readFile(uri);
fileContent = Buffer.from(readData).toString("utf8");
}
if (fileContent) {
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);
return [...arr, newPosition];
}, [] as PositionInFile[]);
return new TopLevel(newPositions, uri);
} else {
return new TopLevel([] as PositionInFile[], uri);
}
})
);
}
Expand All @@ -196,11 +214,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 f501d53

Please sign in to comment.