Skip to content

Commit 0f63b39

Browse files
authored
Merge pull request #915 from CppCXY/format
code reformat
2 parents 6c4fe2e + 96bdc53 commit 0f63b39

File tree

13 files changed

+215
-5
lines changed

13 files changed

+215
-5
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
[submodule "3rd/lovr-api"]
1717
path = 3rd/lovr-api
1818
url = https://github.com/bjornbytes/lovr-docs
19+
[submodule "3rd/EmmyLuaCodeStyle"]
20+
path = 3rd/EmmyLuaCodeStyle
21+
url = https://github.com/CppCXY/EmmyLuaCodeStyle

3rd/EmmyLuaCodeStyle

Submodule EmmyLuaCodeStyle added at ec57661

make.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ elseif platform.OS == 'Linux' then
3737
end
3838

3939
lm:import "3rd/bee.lua/make.lua"
40+
lm:import "make/code_format.lua"
4041

4142
lm:source_set 'lpeglabel' {
4243
rootdir = '3rd',
@@ -48,7 +49,7 @@ lm:source_set 'lpeglabel' {
4849
}
4950

5051
lm:executable "lua-language-server" {
51-
deps = {"lpeglabel", "source_bootstrap"},
52+
deps = {"lpeglabel", "source_bootstrap", "code_format"},
5253
includes = {
5354
"3rd/bee.lua",
5455
"3rd/bee.lua/3rd/lua",

make/code_format.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local lm = require 'luamake'
2+
3+
lm:source_set 'code_format' {
4+
rootdir = '../3rd/EmmyLuaCodeStyle',
5+
includes = {
6+
"include",
7+
"../bee.lua/3rd/lua"
8+
},
9+
sources = {
10+
-- codeFormatLib
11+
"CodeFormatLib/src/*.cpp",
12+
-- LuaParser
13+
"LuaParser/src/*.cpp",
14+
"LuaParser/src/LuaAstNode/LuaAstNode.cpp",
15+
-- Util
16+
"Util/src/StringUtil.cpp",
17+
"Util/src/Utf8.cpp",
18+
--CodeService
19+
"CodeService/src/*.cpp",
20+
"CodeService/src/FormatElement/*.cpp",
21+
"CodeService/src/NameStyle/*.cpp"
22+
},
23+
macos = {
24+
-- macosx10.12不支持完整的std filesystem,只好砍功能
25+
defines = "NOT_SURPPORT_FILE_SYSTEM",
26+
},
27+
}

make/modules.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22

33
extern "C" int luaopen_lpeglabel (lua_State *L);
44
static ::bee::lua::callfunc _init(::bee::lua::register_module, "lpeglabel", luaopen_lpeglabel);
5+
6+
extern "C" int luaopen_code_format(lua_State *L);
7+
static ::bee::lua::callfunc _init_code_format(::bee::lua::register_module, "code_format",
8+
luaopen_code_format);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
local files = require("files")
2+
local codeFormat = require "code_format"
3+
local converter = require("proto.converter")
4+
local log = require("log")
5+
local config = require("config")
6+
7+
8+
---@async
9+
return function(uri, callback)
10+
local text = files.getText(uri)
11+
if not text then
12+
return
13+
end
14+
15+
local status, diagnosticInfos = codeFormat.diagnose_file(uri, text)
16+
17+
if not status then
18+
if diagnosticInfos ~= nil then
19+
log.error(diagnosticInfos)
20+
end
21+
22+
return
23+
end
24+
25+
if diagnosticInfos then
26+
for _, diagnosticInfo in pairs(diagnosticInfos) do
27+
callback {
28+
start = converter.unpackPosition(uri, diagnosticInfo.range.start),
29+
finish = converter.unpackPosition(uri, diagnosticInfo.range["end"]),
30+
message = diagnosticInfo.message
31+
}
32+
end
33+
end
34+
end

script/core/formatting.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
local codeFormat = require("code_format")
2+
local files = require("files")
3+
local log = require("log")
4+
5+
return function(uri)
6+
local text = files.getText(uri)
7+
local ast = files.getState(uri)
8+
local status, formattedText = codeFormat.format(uri, text)
9+
10+
if not status then
11+
if formattedText ~= nil then
12+
log.error(formattedText)
13+
end
14+
15+
return
16+
end
17+
18+
return {
19+
{
20+
start = ast.ast.start,
21+
finish = ast.ast.finish,
22+
text = formattedText,
23+
}
24+
}
25+
end

script/core/rangeformatting.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
local codeFormat = require("code_format")
2+
local files = require("files")
3+
local log = require("log")
4+
local converter = require("proto.converter")
5+
6+
return function(uri, range)
7+
local text = files.getText(uri)
8+
local status, formattedText, startLine, endLine = codeFormat.range_format(
9+
uri, text, range.start.line, range["end"].line)
10+
11+
if not status then
12+
if formattedText ~= nil then
13+
log.error(formattedText)
14+
end
15+
16+
return
17+
end
18+
19+
return {
20+
{
21+
start = converter.unpackPosition(uri, { line = startLine, character = 0 }),
22+
finish = converter.unpackPosition(uri, { line = endLine + 1, character = 0 }),
23+
text = formattedText,
24+
}
25+
}
26+
end

script/core/type-formatting.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ local function checkSplitOneLine(results, uri, position, ch)
4646
if ch ~= '\n' then
4747
return
4848
end
49+
4950
local fPosition, fSymbol = findForward(uri, position, 'end', '}')
5051
if not fPosition then
5152
return
@@ -77,7 +78,7 @@ local function checkSplitOneLine(results, uri, position, ch)
7778
end
7879

7980
return function (uri, position, ch)
80-
local ast = files.getState(uri)
81+
local ast = files.getState(uri)
8182
if not ast then
8283
return nil
8384
end

script/proto/define.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ m.DiagnosticDefaultSeverity = {
5858
['doc-field-no-class'] = 'Warning',
5959
['duplicate-doc-field'] = 'Warning',
6060
['unknown-diag-code'] = 'Waiting',
61+
['codestyle-check'] = "Warning",
6162
}
6263

6364
---@alias DiagnosticDefaultNeededFileStatus

0 commit comments

Comments
 (0)