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
43 changes: 37 additions & 6 deletions denops/@ddu-sources/lsp_references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BaseSource, Context, DduItem, Denops, Item, LSP } from "../ddu_source_l
import { lspRequest, LspResult, Method } from "../ddu_source_lsp/request.ts";
import { Client, ClientName, getClientName, getClients } from "../ddu_source_lsp/client.ts";
import { makePositionParams, TextDocumentPositionParams } from "../ddu_source_lsp/params.ts";
import { getCwd, locationToItem, printError } from "../ddu_source_lsp/util.ts";
import { getCwd, locationToItem, printError, uriToFname } from "../ddu_source_lsp/util.ts";
import { ActionData } from "../@ddu-kinds/lsp.ts";
import { isValidItem } from "../ddu_source_lsp/handler.ts";

Expand All @@ -13,6 +13,7 @@ type ReferenceParams = TextDocumentPositionParams & {
type Params = {
clientName: ClientName | "";
includeDeclaration: boolean;
showLine: boolean;
};

export class Source extends BaseSource<Params> {
Expand All @@ -26,7 +27,7 @@ export class Source extends BaseSource<Params> {
parent?: DduItem;
}): ReadableStream<Item<ActionData>[]> {
const { denops, sourceParams, context: ctx } = args;
const { includeDeclaration } = sourceParams;
const { includeDeclaration, showLine, locationPaddingWidth } = sourceParams;
const method: Method = "textDocument/references";

return new ReadableStream({
Expand All @@ -51,7 +52,7 @@ export class Source extends BaseSource<Params> {
params,
ctx.bufNr,
);
const items = parseResult(result, client, ctx.bufNr, method, cwd);
const items = await parseResult(result, client, ctx.bufNr, method, cwd, denops, showLine, locationPaddingWidth);
controller.enqueue(items);
}));
} catch (e) {
Expand All @@ -67,17 +68,22 @@ export class Source extends BaseSource<Params> {
return {
clientName: "",
includeDeclaration: true,
showLine: false,
locationPaddingWidth: 30
};
}
}

function parseResult(
async function parseResult(
result: LspResult,
client: Client,
bufNr: number,
method: Method,
cwd: string,
): Item<ActionData>[] {
denops: Denops,
showLine: boolean,
locationPaddingWidth: number
): Promise<Item<ActionData>[]> {
/**
* Reference:
* https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
Expand All @@ -89,7 +95,32 @@ function parseResult(

const context = { client, bufNr, method };

// Fetch unique files
const fileContents = new Map<string, string[]>();
if (showLine) {
for (const loc of locations) {
const path = uriToFname(loc.uri);
if (!fileContents.has(path)) {
if (await denops.call("bufloaded", path)) {
fileContents.set(path, await denops.call("getbufline", path, 1, "$") as string[]);
} else {
try {
fileContents.set(path, await denops.call("readfile", path) as string[]);
} catch {
fileContents.set(path, []);
}
}
}
}
}

return locations
.map((location) => locationToItem(location, cwd, context))
.map((location) => {
const path = uriToFname(location.uri);
const line = location.range.start.line;
const content = fileContents.get(path);
const text = content && content[line] ? content[line].trim() : null;
return locationToItem(location, cwd, context, text, locationPaddingWidth);
})
.filter(isValidItem);
}
7 changes: 6 additions & 1 deletion denops/ddu_source_lsp/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@ export function locationToItem(
location: LSP.Location | LSP.LocationLink,
cwd: string,
context: ItemContext,
text: string | null = null,
loc_padding: int | null = null
) {
const uri = "uri" in location ? location.uri : location.targetUri;
const range = "range" in location ? location.range : location.targetSelectionRange;
const path = uriToFname(uri);
const relativePath = relative(cwd, path);
const { line, character } = range.start;
const [lineNr, col] = [line + 1, character + 1];
const display_loc = `${relativePath}:${lineNr}:${col}`
const is_loc_padding_valid = Number.isInteger(loc_padding) && loc_padding > 0
const display = text ? (is_loc_padding_valid ? display_loc.padEnd(loc_padding) + ` ${text}` : display_loc + ` ${text}`) : display_loc;
return {
word: relativePath,
display: `${relativePath}:${lineNr}:${col}`,
display,
action: { path, range, context },
data: location,
};
Expand Down
17 changes: 17 additions & 0 deletions doc/ddu-source-lsp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,23 @@ includeDeclaration (boolean)

Default: true

*ddu-source-lsp-params-showLine*
showLine (boolean)
Available in |ddu-source-lsp_references|.
Whether to show the line content of found references.
Note: Enabling this option may slow down the source because it needs
to read files to get the line content.

Default: false

*ddu-source-lsp-params-locationPaddingWidth*
locationPaddingWidth (number)
Available in |ddu-source-lsp_references|.
If showLine is enabled, add spaces padding to the location text so
that resulting location part has the width of this number.

Default: 30

*ddu-source-lsp-params-query*
query (string)
Available in |ddu-source-lsp_workspaceSymbol|.
Expand Down