Skip to content

Commit

Permalink
feat(language-server): use a config variable to store configuration
Browse files Browse the repository at this point in the history
changes

When using for example the neovim language server client, the contextive
language server does not acquire the proper settings sent by the client
in the `OnStarted` handler. However, the settings are passed correctly
using the `OnDidChangeConfiguration` handler.

This change introduces a scoped variable in the server start function to
hold a mutable field for the Contextive path configuration. The
configuration variable is initialized with the default path for
Contextive definitions introduced in commit 23f9049.

If the language configuration does not have a config value, it will use
the value from the configuration variable instead. This works the same
way as it did previously, but the source is now a parameter of the
function.
  • Loading branch information
erikjuhani committed Oct 21, 2023
1 parent a098580 commit 7a1d36f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
module Contextive.LanguageServer.Configuration

open System.Threading.Tasks
open OmniSharp.Extensions.LanguageServer.Protocol.Models

let resolvedPathGetter configGetter pathResolver () =
async {
let! path = configGetter ()
return pathResolver path
}

let handler (definitionsLoader: Definitions.Reloader) _ =
type Config = { mutable Path: string option }

let handler (config: Config) (definitionsLoader: Definitions.Reloader) (configParams: DidChangeConfigurationParams) =
let path = configParams.Settings.Item("contextive").Item("path")
config.Path <- Some(path.ToString())
definitionsLoader ()
Task.CompletedTask
4 changes: 1 addition & 3 deletions src/language-server/Contextive.LanguageServer/Definitions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ module private Handle =
let matchOpenFileUri = matchGlobs findMsg.OpenFileUri

let foundContexts =
state.Definitions.Contexts
|> Seq.filter matchOpenFileUri
|> findMsg.Filter
state.Definitions.Contexts |> Seq.filter matchOpenFileUri |> findMsg.Filter

findMsg.ReplyChannel.Reply foundContexts
state
Expand Down
19 changes: 11 additions & 8 deletions src/language-server/Contextive.LanguageServer/Server.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let version =
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion

let private getConfig (s: ILanguageServer) section key =
let private getConfig (s: ILanguageServer) (contextiveConfig: Configuration.Config) section key =
async {
Log.Logger.Information $"Getting {section} {key} config..."

Expand All @@ -38,7 +38,7 @@ let private getConfig (s: ILanguageServer) section key =
let value =
if System.String.IsNullOrEmpty configValue then
match key with
| key when key = pathKey -> Some defaultContextiveDefinitionsPath
| key when key = pathKey -> contextiveConfig.Path
| _ -> None
else
Some configValue
Expand Down Expand Up @@ -89,11 +89,13 @@ let private showSurveyPrompt (s: ILanguageServer) =
let private onStartupShowSurveyPrompt =
OnLanguageServerStartedDelegate(fun (s: ILanguageServer) _cancellationToken -> showSurveyPrompt (s))

let private onStartupConfigureServer definitions =
let private onStartupConfigureServer definitions config =
OnLanguageServerStartedDelegate(fun (s: ILanguageServer) _cancellationToken ->
async {
s.Window.LogInfo $"Starting {name} v{version}..."
let configGetter () = getConfig s configSection pathKey

let configGetter () =
getConfig s config configSection pathKey
// Not sure if this is needed to ensure configuration is loaded, or allow a task/context switch
// Either way, if it's not here, then getWorkspaceFolder returns null
let! _ = configGetter ()
Expand Down Expand Up @@ -122,15 +124,16 @@ let private onStartupConfigureServer definitions =
|> Async.StartAsTask
:> Task)


let private configureServer (input: Stream) (output: Stream) (opts: LanguageServerOptions) =
let definitions = Definitions.create ()

let contextiveConfig: Configuration.Config =
{ Path = Some defaultContextiveDefinitionsPath }

opts
.WithInput(input)
.WithOutput(output)

.OnStarted(onStartupConfigureServer definitions)
.OnStarted(onStartupConfigureServer definitions contextiveConfig)
.OnStarted(onStartupShowSurveyPrompt)
.WithConfigurationSection(configSection) // Add back in when implementing didConfigurationChanged handling
.ConfigureLogging(fun z ->
Expand All @@ -141,7 +144,7 @@ let private configureServer (input: Stream) (output: Stream) (opts: LanguageServ
|> ignore)
.WithServerInfo(ServerInfo(Name = name, Version = version))

.OnDidChangeConfiguration(Configuration.handler <| Definitions.loader definitions)
.OnDidChangeConfiguration((Configuration.handler (contextiveConfig)) <| Definitions.loader definitions)
.OnCompletion(
Completion.handler <| Definitions.find definitions <| TextDocument.findToken,
Completion.registrationOptions
Expand Down

0 comments on commit 7a1d36f

Please sign in to comment.