From f8b8e9d61cfa5ff67082510919f5977b55d4eb90 Mon Sep 17 00:00:00 2001 From: ZacNugent Date: Mon, 22 Jul 2019 22:47:33 +0100 Subject: [PATCH] add format option handlers --- src/languageserverinstance.jl | 11 ++++++++++- src/requests/features.jl | 4 ++-- src/requests/init.jl | 2 ++ src/requests/workspace.jl | 8 ++++++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/languageserverinstance.jl b/src/languageserverinstance.jl index 32d91b22..af50d1b8 100644 --- a/src/languageserverinstance.jl +++ b/src/languageserverinstance.jl @@ -16,9 +16,10 @@ mutable struct LanguageServerInstance env_path::String depot_path::String symbol_server::Union{Nothing,SymbolServer.SymbolServerProcess} + format_options::DocumentFormat.FormatOptions function LanguageServerInstance(pipe_in, pipe_out, debug_mode::Bool = false, env_path = "", depot_path = "", packages = Dict()) - new(pipe_in, pipe_out, Set{String}(), Dict{URI2,Document}(), debug_mode, true, Set{String}(), false, packages, env_path, depot_path, nothing) + new(pipe_in, pipe_out, Set{String}(), Dict{URI2,Document}(), debug_mode, true, Set{String}(), false, packages, env_path, depot_path, nothing, DocumentFormat.FormatOptions()) end end @@ -46,6 +47,14 @@ function Base.run(server::LanguageServerInstance) # server.isrunning && serverbusy(server) process(request, server) # server.isrunning && serverready(server) + elseif get(message_dict, "id", 0) == -100 && haskey(message_dict, "result") + # set format options + if length(message_dict["result"]) == length(fieldnames(DocumentFormat.FormatOptions)) + try + server.format_options = DocumentFormat.FormatOptions(message_dict["result"]...) + catch + end + end end end end diff --git a/src/requests/features.jl b/src/requests/features.jl index a7bf5a46..ed7e6d62 100644 --- a/src/requests/features.jl +++ b/src/requests/features.jl @@ -151,13 +151,13 @@ function JSONRPC.parse_params(::Type{Val{Symbol("textDocument/formatting")}}, pa return DocumentFormattingParams(params) end -function process(r::JSONRPC.Request{Val{Symbol("textDocument/formatting")},DocumentFormattingParams}, server) +function process(r::JSONRPC.Request{Val{Symbol("textDocument/formatting")},DocumentFormattingParams}, server::LanguageServerInstance) if !haskey(server.documents, URI2(r.params.textDocument.uri)) send(JSONRPC.Response(r.id, CancelParams(r.id)), server) return end doc = server.documents[URI2(r.params.textDocument.uri)] - newcontent = DocumentFormat.format(doc._content) + newcontent = DocumentFormat.format(doc._content, server.format_options) end_l, end_c = get_position_at(doc, sizeof(doc._content)) lsedits = TextEdit[TextEdit(Range(0, 0, end_l, end_c), newcontent)] diff --git a/src/requests/init.jl b/src/requests/init.jl index 39c399e8..71b1fae9 100644 --- a/src/requests/init.jl +++ b/src/requests/init.jl @@ -101,6 +101,8 @@ function process(r::JSONRPC.Request{Val{Symbol("initialized")}}, server) load_folder(wkspc, server) end end + send(JSONRPC.Request{Val{Symbol("workspace/configuration")},ConfigurationParams}(-100, ConfigurationParams([ + ConfigurationItem(nothing, "julia.format.$opt") for opt in fieldnames(DocumentFormat.FormatOptions)])), server) write_transport_layer(server.pipe_out, JSON.json(Dict("jsonrpc" => "2.0", "id" => "278352324", "method" => "client/registerCapability", "params" => Dict("registrations" => [Dict("id"=>"28c6550c-bd7b-11e7-abc4-cec278b6b50a", "method"=>"workspace/didChangeWorkspaceFolders")]))), server.debug_mode) end diff --git a/src/requests/workspace.jl b/src/requests/workspace.jl index 6c99a3e9..29a77e54 100644 --- a/src/requests/workspace.jl +++ b/src/requests/workspace.jl @@ -29,7 +29,7 @@ function JSONRPC.parse_params(::Type{Val{Symbol("workspace/didChangeConfiguratio end -function process(r::JSONRPC.Request{Val{Symbol("workspace/didChangeConfiguration")},Dict{String,Any}}, server) +function process(r::JSONRPC.Request{Val{Symbol("workspace/didChangeConfiguration")},Dict{String,Any}}, server::LanguageServerInstance) if r.params["settings"] isa Dict && haskey(r.params["settings"], "julia") jsettings = r.params["settings"]["julia"] if haskey(jsettings, "runLinter") && jsettings["runLinter"] != server.runlinter @@ -58,7 +58,11 @@ function process(r::JSONRPC.Request{Val{Symbol("workspace/didChangeConfiguration end end end - + end + if haskey(jsettings, "format") + for (k,v) in jsettings["format"] + setproperty!(server.format_options, Symbol(k), v) + end end end end