Skip to content

Commit

Permalink
client: add command to update server; server: add option for disable …
Browse files Browse the repository at this point in the history
…unusedDeclaration warnings
  • Loading branch information
Yatao Li committed Aug 2, 2019
1 parent a501d66 commit 3d0d16b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 21 deletions.
20 changes: 16 additions & 4 deletions client/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } f
import { NotificationType } from 'vscode-jsonrpc';
import { Range } from 'vscode-languageserver-protocol';
import {OperatingSystem, LanguageServerProvider, ILanguageServerRepository} from './platform'
import {sleep} from './utils';


async function getCurrentSelection(mode: string) {
Expand Down Expand Up @@ -112,7 +113,6 @@ function registerREPL(context: ExtensionContext, __: string) {
return createREPL
}


export async function activate(context: ExtensionContext) {

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

// Register test-runner
commands.registerCommand('fsharp.command.test.run', runTest);
commands.registerCommand('fsharp.command.goto', goto);
// Register commands
context.subscriptions.push(
commands.registerCommand('fsharp.command.test.run', runTest),
commands.registerCommand('fsharp.command.goto', goto),
commands.registerCommand('fsharp.downloadLanguageServer', async () => {
if (client.started) {
await client.stop()
disposable.dispose()
await sleep(1000)
}
await lsprovider.downloadLanguageServer();
disposable = client.start()
context.subscriptions.push(disposable);
}),
)

registerREPL(context, "F# REPL")
}
Expand Down
11 changes: 6 additions & 5 deletions client/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ export class LanguageServerProvider
this.languageServerExe = path.join(this.languageServerDirectory, this.languageServerPackage.executable)
}

private async downloadLanguageServer(): Promise<void> {
public async downloadLanguageServer(): Promise<void> {

let item = workspace.createStatusBarItem(0, {progress: true})
item.text = "Downloading F# Language Server"
item.show()

if(!fs.existsSync(this.extensionStoragePath)) {
fs.mkdirSync(this.extensionStoragePath)
Expand Down Expand Up @@ -146,6 +150,7 @@ export class LanguageServerProvider
})

fs.unlinkSync(this.languageServerZip)
item.dispose()
}

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

if (!fs.existsSync(this.languageServerExe)) {
let item = workspace.createStatusBarItem(0, {progress: true})
item.text = "Downloading F# Language Server"
item.show()
await this.downloadLanguageServer()
item.dispose()
}

// Make sure the server is executable
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@
"type": "boolean",
"default": true,
"description": "Display the number of references for the symbols. Uses more resources and may crash coc.nvim for very large projects."
},
"fsharp.analysis.unusedDeclaration": {
"scope": "resource",
"type": "boolean",
"default": true,
"description": "Enable diagnostic information about unused declarations."
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions src/FSharpLanguageServer/Config.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type FSharpLanguageServerConfig = JsonProvider<"""
},
"codelens": {
"references": true
},
"analysis": {
"unusedDeclaration": true
}
}
}]""", SampleIsList=true>
30 changes: 18 additions & 12 deletions src/FSharpLanguageServer/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ type Server(client: ILanguageClient) as this =
let checker = FSharpChecker.Create()
let projects = ProjectManager(checker)
let mutable codelensShowReferences = true
let mutable showUnusedDeclarations = true

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

/// When did we last check each file on disk?
let lastCheckedOnDisk = dict<string, DateTime>()
// TODO there might be a thread safety issue here---is this getting called from a separate thread?
do checker.BeforeBackgroundFileCheck.Add(fun(fileName, _) ->
let file = FileInfo(fileName)
lastCheckedOnDisk.[file.FullName] <- file.LastWriteTime)
Expand Down Expand Up @@ -344,23 +344,28 @@ type Server(client: ILanguageClient) as this =
| Ok(parseResult, checkResult) ->
let parseErrors = asDiagnostics(parseResult.Errors)
let typeErrors = asDiagnostics(checkResult.Errors)
let! uses = checkResult.GetAllUsesOfAllSymbolsInFile()
fileSymbolUses.[file.FullName] <- Array.map (fun (x: FSharpSymbolUse) -> x.Symbol.FullName) uses
// This is just too slow. Also, it's sometimes wrong.
// Find unused opens
// let timeUnusedOpens = Stopwatch.StartNew()
// let! unusedOpenRanges = UnusedOpens.getUnusedOpens(checkResult, fun(line) -> lineContent(file, line))
// let unusedOpenErrors = [for r in unusedOpenRanges do yield diagnostic("Unused open", r, DiagnosticSeverity.Information)]
// dprintfn "Found %d unused opens in %dms" unusedOpenErrors.Length timeUnusedOpens.ElapsedMilliseconds
let unusedOpenErrors = []

// Find unused declarations
let timeUnusedDeclarations = Stopwatch.StartNew()
let! uses = checkResult.GetAllUsesOfAllSymbolsInFile()
let unusedDeclarationRanges = UnusedDeclarations.getUnusedDeclarationRanges(uses, file.Name.EndsWith(".fsx"))
let unusedDeclarationErrors = [for r in unusedDeclarationRanges do yield diagnostic("Unused declaration", r, DiagnosticSeverity.Hint)]
dprintfn "Found %d unused declarations in %dms" unusedDeclarationErrors.Length timeUnusedDeclarations.ElapsedMilliseconds
let unusedDeclarationErrors =
if showUnusedDeclarations then
let timeUnusedDeclarations = Stopwatch.StartNew()
let unusedDeclarationRanges = UnusedDeclarations.getUnusedDeclarationRanges(uses, file.Name.EndsWith(".fsx"))
let unusedDeclarationErrors = [for r in unusedDeclarationRanges do yield diagnostic("Unused declaration", r, DiagnosticSeverity.Hint)]
dprintfn "Found %d unused declarations in %dms" unusedDeclarationErrors.Length timeUnusedDeclarations.ElapsedMilliseconds
unusedDeclarationErrors
else []

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

Expand Down Expand Up @@ -833,11 +839,11 @@ type Server(client: ILanguageClient) as this =
}

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

0 comments on commit 3d0d16b

Please sign in to comment.