Skip to content

Commit 3d0d16b

Browse files
author
Yatao Li
committed
client: add command to update server; server: add option for disable unusedDeclaration warnings
1 parent a501d66 commit 3d0d16b

File tree

5 files changed

+49
-21
lines changed

5 files changed

+49
-21
lines changed

client/extension.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } f
1212
import { NotificationType } from 'vscode-jsonrpc';
1313
import { Range } from 'vscode-languageserver-protocol';
1414
import {OperatingSystem, LanguageServerProvider, ILanguageServerRepository} from './platform'
15+
import {sleep} from './utils';
1516

1617

1718
async function getCurrentSelection(mode: string) {
@@ -112,7 +113,6 @@ function registerREPL(context: ExtensionContext, __: string) {
112113
return createREPL
113114
}
114115

115-
116116
export async function activate(context: ExtensionContext) {
117117

118118
const cocfs_repo: ILanguageServerRepository = {
@@ -169,9 +169,21 @@ export async function activate(context: ExtensionContext) {
169169
// When the language client activates, register a progress-listener
170170
client.onReady().then(() => createProgressListeners(client));
171171

172-
// Register test-runner
173-
commands.registerCommand('fsharp.command.test.run', runTest);
174-
commands.registerCommand('fsharp.command.goto', goto);
172+
// Register commands
173+
context.subscriptions.push(
174+
commands.registerCommand('fsharp.command.test.run', runTest),
175+
commands.registerCommand('fsharp.command.goto', goto),
176+
commands.registerCommand('fsharp.downloadLanguageServer', async () => {
177+
if (client.started) {
178+
await client.stop()
179+
disposable.dispose()
180+
await sleep(1000)
181+
}
182+
await lsprovider.downloadLanguageServer();
183+
disposable = client.start()
184+
context.subscriptions.push(disposable);
185+
}),
186+
)
175187

176188
registerREPL(context, "F# REPL")
177189
}

client/platform.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ export class LanguageServerProvider
105105
this.languageServerExe = path.join(this.languageServerDirectory, this.languageServerPackage.executable)
106106
}
107107

108-
private async downloadLanguageServer(): Promise<void> {
108+
public async downloadLanguageServer(): Promise<void> {
109+
110+
let item = workspace.createStatusBarItem(0, {progress: true})
111+
item.text = "Downloading F# Language Server"
112+
item.show()
109113

110114
if(!fs.existsSync(this.extensionStoragePath)) {
111115
fs.mkdirSync(this.extensionStoragePath)
@@ -146,6 +150,7 @@ export class LanguageServerProvider
146150
})
147151

148152
fs.unlinkSync(this.languageServerZip)
153+
item.dispose()
149154
}
150155

151156
// returns the full path to the language server executable
@@ -154,11 +159,7 @@ export class LanguageServerProvider
154159
const plat = getPlatformDetails()
155160

156161
if (!fs.existsSync(this.languageServerExe)) {
157-
let item = workspace.createStatusBarItem(0, {progress: true})
158-
item.text = "Downloading F# Language Server"
159-
item.show()
160162
await this.downloadLanguageServer()
161-
item.dispose()
162163
}
163164

164165
// Make sure the server is executable

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@
114114
"type": "boolean",
115115
"default": true,
116116
"description": "Display the number of references for the symbols. Uses more resources and may crash coc.nvim for very large projects."
117+
},
118+
"fsharp.analysis.unusedDeclaration": {
119+
"scope": "resource",
120+
"type": "boolean",
121+
"default": true,
122+
"description": "Enable diagnostic information about unused declarations."
117123
}
118124
}
119125
},

src/FSharpLanguageServer/Config.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ type FSharpLanguageServerConfig = JsonProvider<"""
1414
},
1515
"codelens": {
1616
"references": true
17+
},
18+
"analysis": {
19+
"unusedDeclaration": true
1720
}
1821
}
1922
}]""", SampleIsList=true>

src/FSharpLanguageServer/Program.fs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ type Server(client: ILanguageClient) as this =
211211
let checker = FSharpChecker.Create()
212212
let projects = ProjectManager(checker)
213213
let mutable codelensShowReferences = true
214+
let mutable showUnusedDeclarations = true
214215

215216
/// Get a file from docs, or read it from disk
216217
let getOrRead(file: FileInfo): string option =
@@ -303,7 +304,6 @@ type Server(client: ILanguageClient) as this =
303304

304305
/// When did we last check each file on disk?
305306
let lastCheckedOnDisk = dict<string, DateTime>()
306-
// TODO there might be a thread safety issue here---is this getting called from a separate thread?
307307
do checker.BeforeBackgroundFileCheck.Add(fun(fileName, _) ->
308308
let file = FileInfo(fileName)
309309
lastCheckedOnDisk.[file.FullName] <- file.LastWriteTime)
@@ -344,23 +344,28 @@ type Server(client: ILanguageClient) as this =
344344
| Ok(parseResult, checkResult) ->
345345
let parseErrors = asDiagnostics(parseResult.Errors)
346346
let typeErrors = asDiagnostics(checkResult.Errors)
347+
let! uses = checkResult.GetAllUsesOfAllSymbolsInFile()
348+
fileSymbolUses.[file.FullName] <- Array.map (fun (x: FSharpSymbolUse) -> x.Symbol.FullName) uses
347349
// This is just too slow. Also, it's sometimes wrong.
348350
// Find unused opens
349351
// let timeUnusedOpens = Stopwatch.StartNew()
350352
// let! unusedOpenRanges = UnusedOpens.getUnusedOpens(checkResult, fun(line) -> lineContent(file, line))
351353
// let unusedOpenErrors = [for r in unusedOpenRanges do yield diagnostic("Unused open", r, DiagnosticSeverity.Information)]
352354
// dprintfn "Found %d unused opens in %dms" unusedOpenErrors.Length timeUnusedOpens.ElapsedMilliseconds
355+
let unusedOpenErrors = []
356+
353357
// Find unused declarations
354-
let timeUnusedDeclarations = Stopwatch.StartNew()
355-
let! uses = checkResult.GetAllUsesOfAllSymbolsInFile()
356-
let unusedDeclarationRanges = UnusedDeclarations.getUnusedDeclarationRanges(uses, file.Name.EndsWith(".fsx"))
357-
let unusedDeclarationErrors = [for r in unusedDeclarationRanges do yield diagnostic("Unused declaration", r, DiagnosticSeverity.Hint)]
358-
dprintfn "Found %d unused declarations in %dms" unusedDeclarationErrors.Length timeUnusedDeclarations.ElapsedMilliseconds
358+
let unusedDeclarationErrors =
359+
if showUnusedDeclarations then
360+
let timeUnusedDeclarations = Stopwatch.StartNew()
361+
let unusedDeclarationRanges = UnusedDeclarations.getUnusedDeclarationRanges(uses, file.Name.EndsWith(".fsx"))
362+
let unusedDeclarationErrors = [for r in unusedDeclarationRanges do yield diagnostic("Unused declaration", r, DiagnosticSeverity.Hint)]
363+
dprintfn "Found %d unused declarations in %dms" unusedDeclarationErrors.Length timeUnusedDeclarations.ElapsedMilliseconds
364+
unusedDeclarationErrors
365+
else []
359366

360-
fileSymbolUses.[file.FullName] <- Array.map (fun (x: FSharpSymbolUse) -> x.Symbol.FullName) uses
361367
// Combine
362-
// return parseErrors@typeErrors@unusedOpenErrors@unusedDeclarationErrors
363-
return parseErrors@typeErrors@unusedDeclarationErrors
368+
return parseErrors@typeErrors@unusedOpenErrors@unusedDeclarationErrors
364369
}
365370
let doCheck(file: FileInfo): Async<unit> =
366371
async {
@@ -618,6 +623,7 @@ type Server(client: ILanguageClient) as this =
618623
projects.ConditionalCompilationDefines <- List.ofArray fsconfig.Project.Define
619624
projects.OtherCompilerFlags <- List.ofArray fsconfig.Project.OtherFlags
620625
codelensShowReferences <- fsconfig.Codelens.References
626+
showUnusedDeclarations <- fsconfig.Analysis.UnusedDeclaration
621627
ProjectCracker.includeCompileBeforeItems <- fsconfig.Project.IncludeCompileBefore
622628
dprintfn "New configuration %O" (fsconfig.JsonValue)
623629

@@ -833,11 +839,11 @@ type Server(client: ILanguageClient) as this =
833839
}
834840

835841
/// <summary>
836-
/// TODO match: [fsharp 39: typecheck] [E] The value, namespace, type or module 'Thread' is not defined.
842+
/// match: [fsharp 39: typecheck] [E] The value, namespace, type or module 'Thread' is not defined.
837843
/// then: 1. search a reflection-based cache for a matching entry. if found, suggest opening a module/namespace
838844
/// 2. search for workspace symbol. if found, suggest opening a module/namespace
839-
/// 3. search for nuget packages
840-
/// 4. off-by-one corrections
845+
/// TODO 3. search for nuget packages
846+
/// TODO 4. off-by-one corrections
841847
/// TODO match: [fsharp] [H] Unused declaration
842848
/// then: 1. offer refactoring to _
843849
/// 2. offer refactoring to __

0 commit comments

Comments
 (0)