diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-02-09 15:38:40 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-09 15:38:40 +0800 |
commit | 0f63b39275be9c5c3eb3bca27f5bd660cdc0ae26 (patch) | |
tree | 53b3a597a20deab6e2ff81a727f65e6aaf9d94df /script/provider | |
parent | 6c4fe2e8360b8bee1a6e01d5578e5cfc8191c1ee (diff) | |
parent | 96bdc534fc2c0353e0e21eda7a8deec617f2db33 (diff) | |
download | lua-language-server-0f63b39275be9c5c3eb3bca27f5bd660cdc0ae26.zip |
Merge pull request #915 from CppCXY/format
code reformat
Diffstat (limited to 'script/provider')
-rw-r--r-- | script/provider/capability.lua | 5 | ||||
-rw-r--r-- | script/provider/provider.lua | 86 |
2 files changed, 88 insertions, 3 deletions
diff --git a/script/provider/capability.lua b/script/provider/capability.lua index 8a1424ca..e13e11e0 100644 --- a/script/provider/capability.lua +++ b/script/provider/capability.lua @@ -112,9 +112,8 @@ function m.getIniter() range = true, full = false, }, - --documentOnTypeFormattingProvider = { - -- firstTriggerCharacter = '}', - --}, + documentFormattingProvider = true, + documentRangeFormattingProvider = true } --testFileEvents() diff --git a/script/provider/provider.lua b/script/provider/provider.lua index bd0dd826..778c8b1f 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -102,6 +102,19 @@ m.register 'initialize' { workspace.create(params.rootUri) end + if params.initializationOptions then + if params.initializationOptions.editorConfigFiles then + local codeFormat = require "code_format" + for _, config in pairs(params.initializationOptions.editorConfigFiles) do + local status, err = codeFormat.update_config(1, config.workspace, config.path) + + if not status and err ~= nil then + log.error(err) + end + end + end + end + return { capabilities = cap.getIniter(), serverInfo = { @@ -916,6 +929,79 @@ m.register '$/status/click' { end } +m.register 'textDocument/formatting' { + abortByFileUpdate = true, + ---@async + function(params) + local uri = files.getRealUri(params.textDocument.uri) + workspace.awaitReady(uri) + local _ <close> = progress.create(workspace.getScope(uri), lang.script.WINDOW_PROCESSING_TYPE_FORMATTING, 0.5) + + if not files.exists(uri) then + return nil + end + + local core = require 'core.formatting' + local edits = core(uri) + if not edits or #edits == 0 then + return nil + end + + local results = {} + for i, edit in ipairs(edits) do + results[i] = { + range = converter.packRange(uri, edit.start, edit.finish), + newText = edit.text, + } + end + + return results + end +} + +m.register 'textDocument/rangeFormatting' { + abortByFileUpdate = true, + ---@async + function(params) + local uri = files.getRealUri(params.textDocument.uri) + workspace.awaitReady(uri) + local _ <close> = progress.create(workspace.getScope(uri), lang.script.WINDOW_PROCESSING_TYPE_FORMATTING, 0.5) + + if not files.exists(uri) then + return nil + end + + local core = require 'core.rangeformatting' + local edits = core(uri, params.range) + if not edits or #edits == 0 then + return nil + end + + local results = {} + for i, edit in ipairs(edits) do + results[i] = { + range = converter.packRange(uri, edit.start, edit.finish), + newText = edit.text, + } + end + + return results + end +} + +m.register 'config/editorconfig/update' { + abortByFileUpdate = true, + ---@async + function(params) + local codeFormat = require "code_format" + local status, err = codeFormat.update_config(params.type, params.source.workspace, params.source.path) + + if not status and err ~= nil then + log.error(err) + end + end +} + m.register 'textDocument/onTypeFormatting' { abortByFileUpdate = true, ---@async |