Skip to content

Commit 65799d3

Browse files
authored
Merge pull request #5088 from neoGeneva/Add-AnalyzeOpenDocumentsOnly
Add AnalyzeOpenDocumentsOnly
2 parents c5cd87f + a86bb20 commit 65799d3

File tree

10 files changed

+102
-1
lines changed

10 files changed

+102
-1
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,11 @@
995995
"default": false,
996996
"description": "(EXPERIMENTAL) Enables support for resolving completion edits asynchronously. This can speed up time to show the completion list, particularly override and partial method completion lists, at the cost of slight delays after inserting a completion item. Most completion items will have no noticeable impact with this feature, but typing immediately after inserting an override or partial method completion, before the insert is completed, can have unpredictable results."
997997
},
998+
"omnisharp.analyzeOpenDocumentsOnly": {
999+
"type": "boolean",
1000+
"default": false,
1001+
"description": "Only run analyzers against open files when 'enableRoslynAnalyzers' is true"
1002+
},
9981003
"omnisharp.testRunSettings": {
9991004
"type": [
10001005
"string",
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { IDisposable } from "../Disposable";
2+
import { OmniSharpServer } from "../omnisharp/server";
3+
import * as vscode from 'vscode';
4+
import CompositeDisposable from "../CompositeDisposable";
5+
import * as serverUtils from '../omnisharp/utils';
6+
import { isVirtualCSharpDocument } from "./virtualDocumentTracker";
7+
8+
export default function fileOpenClose(server: OmniSharpServer): IDisposable {
9+
return new FileOpenCloseProvider(server);
10+
}
11+
12+
class FileOpenCloseProvider implements IDisposable {
13+
private _server: OmniSharpServer;
14+
private _diagnostics: vscode.DiagnosticCollection;
15+
private _disposable: CompositeDisposable;
16+
17+
constructor(server: OmniSharpServer) {
18+
this._server = server;
19+
this._diagnostics = vscode.languages.createDiagnosticCollection('csharp');
20+
21+
setTimeout(async () => {
22+
for (let editor of vscode.window.visibleTextEditors) {
23+
let document = editor.document;
24+
25+
await this._onDocumentOpen(document);
26+
}
27+
}, 0);
28+
29+
this._disposable = new CompositeDisposable(this._diagnostics,
30+
vscode.workspace.onDidOpenTextDocument(this._onDocumentOpen, this),
31+
vscode.workspace.onDidCloseTextDocument(this._onDocumentClose, this),
32+
vscode.window.onDidChangeActiveTextEditor(this._onActiveTextEditorChange, this)
33+
);
34+
}
35+
36+
private async _onDocumentOpen(e: vscode.TextDocument) {
37+
if (shouldIgnoreDocument(e)) {
38+
return;
39+
}
40+
41+
await serverUtils.fileOpen(this._server, { FileName: e.fileName });
42+
}
43+
44+
private async _onDocumentClose(e: vscode.TextDocument) {
45+
if (shouldIgnoreDocument(e)) {
46+
return;
47+
}
48+
49+
await serverUtils.fileClose(this._server, { FileName: e.fileName });
50+
}
51+
52+
private async _onActiveTextEditorChange(e: vscode.TextEditor) {
53+
if (shouldIgnoreDocument(e.document)) {
54+
return;
55+
}
56+
57+
await serverUtils.filesChanged(this._server, [{ FileName: e.document.fileName }]);
58+
}
59+
60+
dispose = () => this._disposable.dispose();
61+
}
62+
63+
function shouldIgnoreDocument(document: vscode.TextDocument) {
64+
if (document.languageId !== 'csharp') {
65+
return true;
66+
}
67+
68+
if (document.uri.scheme !== 'file' &&
69+
!isVirtualCSharpDocument(document)) {
70+
return true;
71+
}
72+
73+
return false;
74+
}

src/observers/OptionChangeObserver.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const omniSharpOptions: ReadonlyArray<OptionsKey> = [
2424
"organizeImportsOnFormat",
2525
"enableAsyncCompletion",
2626
"useModernNet",
27+
"analyzeOpenDocumentsOnly",
28+
"enableRoslynAnalyzers"
2729
];
2830

2931
function OmniSharpOptionChangeObservable(optionObservable: Observable<Options>): Observable<Options> {

src/omnisharp/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import SemanticTokensProvider from '../features/semanticTokensProvider';
4444
import SourceGeneratedDocumentProvider from '../features/sourceGeneratedDocumentProvider';
4545
import { getDecompilationAuthorization } from './decompilationPrompt';
4646
import { OmniSharpDotnetResolver } from './OmniSharpDotnetResolver';
47+
import fileOpenClose from '../features/fileOpenCloseProvider';
4748

4849
export interface ActivationResult {
4950
readonly server: OmniSharpServer;
@@ -111,6 +112,7 @@ export async function activate(context: vscode.ExtensionContext, packageJSON: an
111112
localDisposables.add(forwardChanges(server));
112113
localDisposables.add(trackVirtualDocuments(server, eventStream));
113114
localDisposables.add(vscode.languages.registerFoldingRangeProvider(documentSelector, new StructureProvider(server, languageMiddlewareFeature)));
115+
localDisposables.add(fileOpenClose(server));
114116

115117
const semanticTokensProvider = new SemanticTokensProvider(server, optionProvider, languageMiddlewareFeature);
116118
localDisposables.add(vscode.languages.registerDocumentSemanticTokensProvider(documentSelector, semanticTokensProvider, semanticTokensProvider.getLegend()));

src/omnisharp/options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class Options {
3434
public enableDecompilationSupport: boolean,
3535
public enableImportCompletion: boolean,
3636
public enableAsyncCompletion: boolean,
37+
public analyzeOpenDocumentsOnly: boolean,
3738
public useSemanticHighlighting: boolean,
3839
public razorPluginPath?: string,
3940
public defaultLaunchSolution?: string,
@@ -82,6 +83,7 @@ export class Options {
8283
const enableDecompilationSupport = omnisharpConfig.get<boolean>('enableDecompilationSupport', false);
8384
const enableImportCompletion = omnisharpConfig.get<boolean>('enableImportCompletion', false);
8485
const enableAsyncCompletion = omnisharpConfig.get<boolean>('enableAsyncCompletion', false);
86+
const analyzeOpenDocumentsOnly = omnisharpConfig.get<boolean>('analyzeOpenDocumentsOnly', false);
8587

8688
const useFormatting = csharpConfig.get<boolean>('format.enable', true);
8789
const organizeImportsOnFormat = omnisharpConfig.get<boolean>('organizeImportsOnFormat', false);
@@ -141,6 +143,7 @@ export class Options {
141143
enableDecompilationSupport,
142144
enableImportCompletion,
143145
enableAsyncCompletion,
146+
analyzeOpenDocumentsOnly,
144147
useSemanticHighlighting,
145148
razorPluginPath,
146149
defaultLaunchSolution,

src/omnisharp/protocol.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export module Requests {
3737
export const SourceGeneratedFile = '/sourcegeneratedfile';
3838
export const UpdateSourceGeneratedFile = '/updatesourcegeneratedfile';
3939
export const SourceGeneratedFileClosed = '/sourcegeneratedfileclosed';
40+
export const FileOpen = '/open';
41+
export const FileClose = '/close';
4042
}
4143

4244
export namespace WireProtocol {

src/omnisharp/server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ export class OmniSharpServer {
391391
args.push('RoslynExtensionsOptions:EnableAsyncCompletion=true');
392392
}
393393

394+
if (options.analyzeOpenDocumentsOnly === true) {
395+
args.push('RoslynExtensionsOptions:AnalyzeOpenDocumentsOnly=true');
396+
}
397+
394398
let launchInfo: LaunchInfo;
395399
try {
396400
launchInfo = await this._omnisharpManager.GetOmniSharpLaunchInfo(this.packageJSON.defaults.omniSharp, options.path, /* useFramework */ !options.useModernNet, serverUrl, latestVersionFileServerPath, installPath, this.extensionPath);

src/omnisharp/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ export async function getCompletionAfterInsert(server: OmniSharpServer, request:
193193
return server.makeRequest<protocol.CompletionAfterInsertResponse>(protocol.Requests.CompletionAfterInsert, request);
194194
}
195195

196+
export async function fileOpen(server: OmniSharpServer, request: protocol.Request) {
197+
return server.makeRequest<void>(protocol.Requests.FileOpen, request);
198+
}
199+
200+
export async function fileClose(server: OmniSharpServer, request: protocol.Request) {
201+
return server.makeRequest<void>(protocol.Requests.FileClose, request);
202+
}
203+
196204
export async function isNetCoreProject(project: protocol.MSBuildProject) {
197205
return project.TargetFrameworks.find(tf => tf.ShortName.startsWith('netcoreapp') || tf.ShortName.startsWith('netstandard')) !== undefined;
198206
}

test/unitTests/Fakes/FakeOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
import { Options } from "../../../src/omnisharp/options";
77

88
export function getEmptyOptions(): Options {
9-
return new Options("", false, "", false, "", false, 0, 0, false, false, false, false, false, [], false, false, false, 0, 0, false, false, false, false, false, false, false, false, undefined, "", "", "");
9+
return new Options("", false, "", false, "", false, 0, 0, false, false, false, false, false, [], false, false, false, 0, 0, false, false, false, false, false, false, false, false, false, undefined, "", "", "");
1010
}

test/unitTests/options.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ suite("Options tests", () => {
3434
options.enableEditorConfigSupport.should.equal(false);
3535
options.enableDecompilationSupport.should.equal(false);
3636
options.enableImportCompletion.should.equal(false);
37+
options.analyzeOpenDocumentsOnly.should.equal(false);
3738
expect(options.testRunSettings).to.be.undefined;
3839
expect(options.defaultLaunchSolution).to.be.undefined;
3940
});

0 commit comments

Comments
 (0)