Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions 3rd/EmmyLuaCodeStyle
Submodule EmmyLuaCodeStyle added at ec5766
3 changes: 2 additions & 1 deletion make.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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",
Expand Down
27 changes: 27 additions & 0 deletions make/code_format.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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"
},
macos = {
-- macosx10.12不支持完整的std filesystem,只好砍功能
defines = "NOT_SURPPORT_FILE_SYSTEM",
},
}
4 changes: 4 additions & 0 deletions make/modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
34 changes: 34 additions & 0 deletions script/core/diagnostics/codestyle-check.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local files = require("files")
local codeFormat = require "code_format"
local converter = require("proto.converter")
local log = require("log")
local config = require("config")


---@async
return function(uri, callback)
local text = files.getText(uri)
if not text then
return
end

local status, diagnosticInfos = codeFormat.diagnose_file(uri, text)

if not status then
if diagnosticInfos ~= nil then
log.error(diagnosticInfos)
end

return
end

if diagnosticInfos then
for _, diagnosticInfo in pairs(diagnosticInfos) do
callback {
start = converter.unpackPosition(uri, diagnosticInfo.range.start),
finish = converter.unpackPosition(uri, diagnosticInfo.range["end"]),
message = diagnosticInfo.message
}
end
end
end
25 changes: 25 additions & 0 deletions script/core/formatting.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
local codeFormat = 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 = codeFormat.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
26 changes: 26 additions & 0 deletions script/core/rangeformatting.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local codeFormat = require("code_format")
local files = require("files")
local log = require("log")
local converter = require("proto.converter")

return function(uri, range)
local text = files.getText(uri)
local status, formattedText, startLine, endLine = codeFormat.range_format(
uri, text, range.start.line, range["end"].line)

if not status then
if formattedText ~= nil then
log.error(formattedText)
end

return
end

return {
{
start = converter.unpackPosition(uri, { line = startLine, character = 0 }),
finish = converter.unpackPosition(uri, { line = endLine + 1, character = 0 }),
text = formattedText,
}
}
end
3 changes: 2 additions & 1 deletion script/core/type-formatting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ local function checkSplitOneLine(results, uri, position, ch)
if ch ~= '\n' then
return
end

local fPosition, fSymbol = findForward(uri, position, 'end', '}')
if not fPosition then
return
Expand Down Expand Up @@ -77,7 +78,7 @@ local function checkSplitOneLine(results, uri, position, ch)
end

return function (uri, position, ch)
local ast = files.getState(uri)
local ast = files.getState(uri)
if not ast then
return nil
end
Expand Down
1 change: 1 addition & 0 deletions script/proto/define.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ m.DiagnosticDefaultSeverity = {
['doc-field-no-class'] = 'Warning',
['duplicate-doc-field'] = 'Warning',
['unknown-diag-code'] = 'Waiting',
['codestyle-check'] = "Warning",
}

---@alias DiagnosticDefaultNeededFileStatus
Expand Down
5 changes: 2 additions & 3 deletions script/provider/capability.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ function m.getIniter()
range = true,
full = false,
},
--documentOnTypeFormattingProvider = {
-- firstTriggerCharacter = '}',
--},
documentFormattingProvider = true,
documentRangeFormattingProvider = true
}

--testFileEvents()
Expand Down
86 changes: 86 additions & 0 deletions script/provider/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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 = {
Expand Down Expand Up @@ -911,6 +924,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
Expand Down
2 changes: 2 additions & 0 deletions test/crossfile/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ local config = require 'config'
local platform = require 'bee.platform'
local catch = require 'catch'


config.get(nil, 'Lua.diagnostics.neededFileStatus')['deprecated'] = 'Any'
config.get(nil, 'Lua.diagnostics.neededFileStatus')['type-check'] = 'Any'
config.get(nil, 'Lua.diagnostics.neededFileStatus')['codestyle-check'] = 'None'

rawset(_G, 'TEST', true)

Expand Down