Skip to content
Merged
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
4 changes: 3 additions & 1 deletion src/__mocks__/vscode.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const workspace = {
},

getWorkspaceFolder: (_uri) => {
return _scope;
if (_uri.fsPath.startsWith(_scope.uri.fsPath)) {
return _scope;
}
},
};

Expand Down
13 changes: 8 additions & 5 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const vscode = require("vscode");
const { exec } = require('child_process');
const { promisify } = require('util');
const { isAbsolute } = require('path');

const { EXTENSION_ID, EXTENSION_NAME } = require("./constants");

/**
* @param {vscode.WorkspaceFolder | null} scope
* @param {vscode.WorkspaceFolder | undefined} scope
* @returns {vscode.WorkspaceConfiguration}
*/
function getConfiguration(scope = null) {
function getConfiguration(scope = undefined) {
return vscode.workspace.getConfiguration(EXTENSION_ID, scope);
}

Expand Down Expand Up @@ -87,13 +88,15 @@ const SYMBOL_KINDS = {

/**
* @param {string} definition
* @param {vscode.WorkspaceFolder} scope
* @param {vscode.WorkspaceFolder | undefined} scope
* @returns {vscode.SymbolInformation}
*/
function definitionToSymbolInformation(definition, scope) {
function definitionToSymbolInformation(definition, scope = undefined) {
const [symbol, path, ...fields] = definition.split("\t");

const file = path.startsWith('/') ? vscode.Uri.parse(path) : vscode.Uri.joinPath(scope.uri, path);
const file = isAbsolute(path) || scope === undefined
? vscode.Uri.parse(path)
: vscode.Uri.joinPath(scope.uri, path);
Comment thread
gediminasz marked this conversation as resolved.

const lineStr = findField(fields, "line:");
const line = lineStr ? parseInt(lineStr, 10) - 1 : 0;
Expand Down
11 changes: 2 additions & 9 deletions src/readtags.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class ReadtagsProvider {

const definitions = await this.exec(`${command} '${symbol}'`, { cwd });
return definitions
// @ts-expect-error
.map(d => definitionToSymbolInformation(d, scope))
.map(({ location }) => location);
}
Expand Down Expand Up @@ -59,14 +58,8 @@ class ReadtagsProvider {
async provideDocumentSymbols(document) {
const scope = vscode.workspace.getWorkspaceFolder(document.uri);
const command = getConfiguration(scope).get("ctagsGoToSymbolInEditorCommand");
const relativePath = vscode.workspace.asRelativePath(document.uri, false);
// TODO FIXME scope is undefined when editing a file that is outside of workspace, e.g. it is on desktop or so
// @ts-expect-error
const cwd = scope.uri.fsPath;

const definitions = await this.exec(`${command} '${relativePath}'`, { cwd });
// @ts-expect-error
return definitions.map(d => definitionToSymbolInformation(d, scope));
const definitions = await this.exec(`${command} '${document.uri.fsPath}'`, {});
return definitions.map(d => definitionToSymbolInformation(d));
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/readtags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ describe(ReadtagsProvider, () => {
});

describe("provideDocumentSymbols", () => {
it("returns symbol location", async () => {
it.each([
["document within workspace", document],
["document outside workspace", { uri: { fsPath: "/tmp/test.txt" } }],
])("returns symbol location for %s", async (_, document) => {
const provider = new ReadtagsProvider(exec);
const definitions = await provider.provideDocumentSymbols(document);

Expand All @@ -90,7 +93,7 @@ describe(ReadtagsProvider, () => {
kind: vscode.SymbolKind.Function,
containerName: "",
location: {
uri: "/test/include/linux/bitmap.h",
uri: "include/linux/bitmap.h",
rangeOrPosition: {
line: 353,
character: 0
Expand Down