diff options
-rw-r--r-- | .gitmodules | 3 | ||||
m--------- | 3rd/EmmyLuaCodeStyle | 0 | ||||
-rw-r--r-- | make.lua | 3 | ||||
-rw-r--r-- | make/code_format.lua | 23 | ||||
-rw-r--r-- | make/modules.cpp | 4 | ||||
-rw-r--r-- | script/core/formatting.lua | 25 | ||||
-rw-r--r-- | script/provider/capability.lua | 1 | ||||
-rw-r--r-- | script/provider/provider.lua | 56 |
8 files changed, 114 insertions, 1 deletions
diff --git a/.gitmodules b/.gitmodules index 0a1d17a8..454056b0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "3rd/lovr-api"] path = 3rd/lovr-api url = https://github.com/bjornbytes/lovr-docs +[submodule "3rd/EmmyLuaCodeStyle"] + path = 3rd/EmmyLuaCodeStyle + url = https://github.com/CppCXY/EmmyLuaCodeStyle diff --git a/3rd/EmmyLuaCodeStyle b/3rd/EmmyLuaCodeStyle new file mode 160000 +Subproject 45e84d27a28023c1186c741658e0df4f0746bd0 @@ -37,6 +37,7 @@ elseif platform.OS == 'Linux' then end lm:import "3rd/bee.lua/make.lua" +lm:import "make/code_format.lua" lm:source_set 'lpeglabel' { rootdir = '3rd', @@ -48,7 +49,7 @@ lm:source_set 'lpeglabel' { } lm:executable "lua-language-server" { - deps = {"lpeglabel", "source_bootstrap"}, + deps = {"lpeglabel", "source_bootstrap", "code_format"}, includes = { "3rd/bee.lua", "3rd/bee.lua/3rd/lua", diff --git a/make/code_format.lua b/make/code_format.lua new file mode 100644 index 00000000..11b97e4a --- /dev/null +++ b/make/code_format.lua @@ -0,0 +1,23 @@ +local lm = require 'luamake' + +lm:source_set 'code_format' { + rootdir = '../3rd/EmmyLuaCodeStyle', + includes = { + "include", + "../bee.lua/3rd/lua" + }, + sources = { + -- codeFormatLib + "CodeFormatLib/src/*.cpp", + -- LuaParser + "LuaParser/src/*.cpp", + "LuaParser/src/LuaAstNode/LuaAstNode.cpp", + -- Util + "Util/src/StringUtil.cpp", + "Util/src/Utf8.cpp", + --CodeService + "CodeService/src/*.cpp", + "CodeService/src/FormatElement/*.cpp", + "CodeService/src/NameStyle/*.cpp" + } +} diff --git a/make/modules.cpp b/make/modules.cpp index f72fc058..8fe065a8 100644 --- a/make/modules.cpp +++ b/make/modules.cpp @@ -2,3 +2,7 @@ extern "C" int luaopen_lpeglabel (lua_State *L); static ::bee::lua::callfunc _init(::bee::lua::register_module, "lpeglabel", luaopen_lpeglabel); + +extern "C" int luaopen_code_format(lua_State *L); +static ::bee::lua::callfunc _init_code_format(::bee::lua::register_module, "code_format", + luaopen_code_format); diff --git a/script/core/formatting.lua b/script/core/formatting.lua new file mode 100644 index 00000000..8f72028b --- /dev/null +++ b/script/core/formatting.lua @@ -0,0 +1,25 @@ +local code_format = require "code_format" +local files = require("files") +local log = require("log") + +return function(uri) + local text = files.getText(uri) + local ast = files.getState(uri) + local status, formattedText = code_format.format(uri, text) + + if not status then + if formattedText ~= nil then + log.error(formattedText) + end + + return + end + + return { + { + start = ast.ast.start, + finish = ast.ast.finish, + text = formattedText, + } + } +end diff --git a/script/provider/capability.lua b/script/provider/capability.lua index 8a1424ca..27950d7f 100644 --- a/script/provider/capability.lua +++ b/script/provider/capability.lua @@ -112,6 +112,7 @@ function m.getIniter() range = true, full = false, }, + documentFormattingProvider = true --documentOnTypeFormattingProvider = { -- firstTriggerCharacter = '}', --}, diff --git a/script/provider/provider.lua b/script/provider/provider.lua index 4b22cb47..63450f59 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -101,6 +101,19 @@ m.register 'initialize' { workspace.create(params.rootUri) end + if params.initializationOptions then + if params.initializationOptions.editorConfigFiles then + local code_format = require "code_format" + for _, config in pairs(params.initializationOptions.editorConfigFiles) do + local status, err = code_format.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 = { @@ -911,6 +924,49 @@ 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 'config/editorconfig/update' { + abortByFileUpdate = true, + ---@async + function(params) + local code_format = require "code_format" + local status, err = code_format.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 |