Skip to content

Commit 86c2adf

Browse files
committed
Add setting 'java.references.includeDeclarations'.
- Control inclusion of declarations when searching for references - Also simplify the 'resolveCodeAction' middleware implementation Signed-off-by: Roland Grunberg <[email protected]>
1 parent 2cd1c83 commit 86c2adf

File tree

3 files changed

+59
-46
lines changed

3 files changed

+59
-46
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ The following settings are supported:
256256
* `java.jdt.ls.javac.enabled`: [Experimental] Specify whether to enable Javac-based compilation in the language server. Requires running this extension with Java 23. Defaults to `off`.
257257
* `java.completion.engine`: [Experimental] Select code completion engine. Defaults to `ecj`.
258258

259+
New in 1.37.0
260+
* `java.references.includeDeclarations`: Include declarations when finding references. Defaults to `true`
261+
259262
Semantic Highlighting
260263
===============
261264
[Semantic Highlighting](https://github.com/redhat-developer/vscode-java/wiki/Semantic-Highlighting) fixes numerous syntax highlighting issues with the default Java Textmate grammar. However, you might experience a few minor issues, particularly a delay when it kicks in, as it needs to be computed by the Java Language server, when opening a new file or when typing. Semantic highlighting can be disabled for all languages using the `editor.semanticHighlighting.enabled` setting, or for Java only using [language-specific editor settings](https://code.visualstudio.com/docs/getstarted/settings#_languagespecific-editor-settings).

package.json

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,26 +1422,33 @@
14221422
"scope": "window",
14231423
"order": 30
14241424
},
1425+
"java.references.includeDeclarations": {
1426+
"type": "boolean",
1427+
"default": true,
1428+
"description": "Include declarations when finding references.",
1429+
"scope": "window",
1430+
"order": 40
1431+
},
14251432
"java.references.includeDecompiledSources": {
14261433
"type": "boolean",
14271434
"default": true,
14281435
"description": "Include the decompiled sources when finding references.",
14291436
"scope": "window",
1430-
"order": 40
1437+
"order": 50
14311438
},
14321439
"java.symbols.includeSourceMethodDeclarations": {
14331440
"type": "boolean",
14341441
"markdownDescription": "Include method declarations from source files in symbol search.",
14351442
"default": false,
14361443
"scope": "window",
1437-
"order": 50
1444+
"order": 60
14381445
},
14391446
"java.typeHierarchy.lazyLoad": {
14401447
"type": "boolean",
14411448
"default": false,
14421449
"description": "Enable/disable lazy loading the content in type hierarchy. Lazy loading could save a lot of loading time but every type should be expanded manually to load its content.",
14431450
"scope": "window",
1444-
"order": 60
1451+
"order": 70
14451452
},
14461453
"java.inlayHints.parameterNames.enabled": {
14471454
"type": "string",
@@ -1458,7 +1465,7 @@
14581465
"default": "literals",
14591466
"markdownDescription": "Enable/disable inlay hints for parameter names:\n```java\n\nInteger.valueOf(/* s: */ '123', /* radix: */ 10)\n \n```\n `#java.inlayHints.parameterNames.exclusions#` can be used to disable the inlay hints for methods.",
14601467
"scope": "window",
1461-
"order": 70
1468+
"order": 80
14621469
},
14631470
"java.inlayHints.parameterNames.exclusions": {
14641471
"type": "array",
@@ -1468,7 +1475,7 @@
14681475
"default": [],
14691476
"markdownDescription": "The patterns for the methods that will be disabled to show the inlay hints. Supported pattern examples:\n - `java.lang.Math.*` - All the methods from java.lang.Math.\n - `*.Arrays.asList` - Methods named as 'asList' in the types named as 'Arrays'.\n - `*.println(*)` - Methods named as 'println'.\n - `(from, to)` - Methods with two parameters named as 'from' and 'to'.\n - `(arg*)` - Methods with one parameter whose name starts with 'arg'.",
14701477
"scope": "window",
1471-
"order": 80
1478+
"order": 90
14721479
},
14731480
"java.search.scope": {
14741481
"type": "string",
@@ -1483,7 +1490,7 @@
14831490
"default": "all",
14841491
"markdownDescription": "Specifies the scope which must be used for search operation like \n - Find Reference\n - Call Hierarchy\n - Workspace Symbols",
14851492
"scope": "window",
1486-
"order": 90
1493+
"order": 100
14871494
}
14881495
}
14891496
},

src/extension.ts

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as fse from 'fs-extra';
66
import * as os from 'os';
77
import * as path from 'path';
88
import * as semver from 'semver';
9-
import { CodeActionContext, commands, CompletionItem, ConfigurationTarget, Diagnostic, env, EventEmitter, ExtensionContext, extensions, IndentAction, InputBoxOptions, languages, MarkdownString, QuickPickItemKind, Range, RelativePattern, SnippetString, SnippetTextEdit, TextDocument, TextEditorRevealType, UIKind, Uri, ViewColumn, window, workspace, WorkspaceConfiguration, WorkspaceEdit } from 'vscode';
9+
import { CodeActionContext, commands, CompletionItem, ConfigurationTarget, Diagnostic, env, EventEmitter, ExtensionContext, extensions, IndentAction, InputBoxOptions, languages, Location, MarkdownString, QuickPickItemKind, Range, RelativePattern, SnippetString, SnippetTextEdit, TextDocument, TextEditorRevealType, UIKind, Uri, ViewColumn, window, workspace, WorkspaceConfiguration, WorkspaceEdit } from 'vscode';
1010
import { CancellationToken, CodeActionParams, CodeActionRequest, CodeActionResolveRequest, Command, CompletionRequest, DidChangeConfigurationNotification, ExecuteCommandParams, ExecuteCommandRequest, LanguageClientOptions, RevealOutputChannelOn } from 'vscode-languageclient';
1111
import { LanguageClient } from 'vscode-languageclient/node';
1212
import { apiManager } from './apiManager';
@@ -307,54 +307,57 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>
307307
const client: LanguageClient = standardClient.getClient();
308308
const documentUris = [];
309309
const snippetEdits = [];
310-
const resolveCodeAction = async (item, token) => {
311-
return client.sendRequest(CodeActionResolveRequest.type, client.code2ProtocolConverter.asCodeActionSync(item), token).then(async (result) => {
312-
if (token.isCancellationRequested) {
313-
return item;
314-
}
315-
const docChanges = result.edit !== undefined ? result.edit.documentChanges : undefined;
316-
if (docChanges !== undefined) {
317-
for (const docChange of docChanges) {
318-
if ("textDocument" in docChange) {
319-
for (const edit of docChange.edits) {
320-
if ("snippet" in edit) {
321-
documentUris.push(Uri.parse(docChange.textDocument.uri).toString());
322-
snippetEdits.push(new SnippetTextEdit(client.protocol2CodeConverter.asRange((edit as any).range), new SnippetString((edit as any).snippet.value)));
323-
}
310+
return client.sendRequest(CodeActionResolveRequest.type, client.code2ProtocolConverter.asCodeActionSync(item), token).then(async (result) => {
311+
if (token.isCancellationRequested) {
312+
return item;
313+
}
314+
const docChanges = result.edit !== undefined ? result.edit.documentChanges : undefined;
315+
if (docChanges !== undefined) {
316+
for (const docChange of docChanges) {
317+
if ("textDocument" in docChange) {
318+
for (const edit of docChange.edits) {
319+
if ("snippet" in edit) {
320+
documentUris.push(Uri.parse(docChange.textDocument.uri).toString());
321+
snippetEdits.push(new SnippetTextEdit(client.protocol2CodeConverter.asRange((edit as any).range), new SnippetString((edit as any).snippet.value)));
324322
}
325323
}
326324
}
327-
const codeAction = await client.protocol2CodeConverter.asCodeAction(result, token);
328-
const docEdits = codeAction.edit !== undefined? codeAction.edit.entries() : [];
329-
for (const docEdit of docEdits) {
330-
const uri = docEdit[0];
331-
if (documentUris.includes(uri.toString())) {
332-
const editList = [];
333-
for (const edit of docEdit[1]) {
334-
let isSnippet = false;
335-
snippetEdits.forEach((snippet, index) => {
336-
if (edit.range.isEqual(snippet.range) && documentUris[index] === uri.toString()) {
337-
editList.push(snippet);
338-
isSnippet = true;
339-
}
340-
});
341-
if (!isSnippet) {
342-
editList.push(edit);
325+
}
326+
const codeAction = await client.protocol2CodeConverter.asCodeAction(result, token);
327+
const docEdits = codeAction.edit !== undefined? codeAction.edit.entries() : [];
328+
for (const docEdit of docEdits) {
329+
const uri = docEdit[0];
330+
if (documentUris.includes(uri.toString())) {
331+
const editList = [];
332+
for (const edit of docEdit[1]) {
333+
let isSnippet = false;
334+
snippetEdits.forEach((snippet, index) => {
335+
if (edit.range.isEqual(snippet.range) && documentUris[index] === uri.toString()) {
336+
editList.push(snippet);
337+
isSnippet = true;
343338
}
339+
});
340+
if (!isSnippet) {
341+
editList.push(edit);
344342
}
345-
codeAction.edit.set(uri, null);
346-
codeAction.edit.set(uri, editList);
347343
}
344+
codeAction.edit.set(uri, null);
345+
codeAction.edit.set(uri, editList);
348346
}
349-
return codeAction;
350347
}
351-
return await client.protocol2CodeConverter.asCodeAction(result, token);
352-
}, (error) => {
353-
return client.handleFailedRequest(CodeActionResolveRequest.type, token, error, item);
354-
});
355-
};
356-
return resolveCodeAction(item, token);
348+
return codeAction;
349+
}
350+
return await client.protocol2CodeConverter.asCodeAction(result, token);
351+
}, (error) => {
352+
return client.handleFailedRequest(CodeActionResolveRequest.type, token, error, item);
353+
});
357354
},
355+
356+
provideReferences: async(document, position, options, token, next): Promise<Location[]> => {
357+
// Override includeDeclaration from VS Code by allowing it to be configured
358+
options.includeDeclaration = getJavaConfiguration().get('references.includeDeclarations');
359+
return await next(document, position, options, token);
360+
}
358361
},
359362
revealOutputChannelOn: RevealOutputChannelOn.Never,
360363
errorHandler: new ClientErrorHandler(extensionName),

0 commit comments

Comments
 (0)