Skip to content

Commit 92b00f4

Browse files
committed
fix #950
1 parent 8521ec0 commit 92b00f4

File tree

7 files changed

+142
-106
lines changed

7 files changed

+142
-106
lines changed

script/core/diagnostics/codestyle-check.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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")
1+
local files = require 'files'
2+
local codeFormat = require 'code_format'
3+
local converter = require 'proto.converter'
4+
local log = require 'log'
5+
local pformatting = require 'provider.formatting'
66

77

88
---@async
@@ -12,6 +12,8 @@ return function(uri, callback)
1212
return
1313
end
1414

15+
pformatting.updateConfig(uri)
16+
1517
local status, diagnosticInfos = codeFormat.diagnose_file(uri, text)
1618

1719
if not status then
@@ -21,12 +23,12 @@ return function(uri, callback)
2123

2224
return
2325
end
24-
26+
2527
if diagnosticInfos then
26-
for _, diagnosticInfo in pairs(diagnosticInfos) do
28+
for _, diagnosticInfo in ipairs(diagnosticInfos) do
2729
callback {
28-
start = converter.unpackPosition(uri, diagnosticInfo.range.start),
29-
finish = converter.unpackPosition(uri, diagnosticInfo.range["end"]),
30+
start = converter.unpackPosition(uri, diagnosticInfo.range.start),
31+
finish = converter.unpackPosition(uri, diagnosticInfo.range["end"]),
3032
message = diagnosticInfo.message
3133
}
3234
end

script/filewatch.lua

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ function m.event(callback)
5454
m._eventList[#m._eventList+1] = callback
5555
end
5656

57-
function m._callEvent(changes)
57+
function m._callEvent(ev, path)
5858
for _, callback in ipairs(m._eventList) do
5959
await.call(function ()
60-
callback(changes)
60+
callback(ev, path)
6161
end)
6262
end
6363
end
@@ -83,29 +83,18 @@ function m.update()
8383
return
8484
end
8585

86-
local changes = {}
8786
for path, flag in pairs(collect) do
8887
if flag & RENAME ~= 0 then
8988
if exists(path) then
90-
changes[#changes+1] = {
91-
type = 'create',
92-
path = path,
93-
}
89+
m._callEvent('create', path)
9490
else
95-
changes[#changes+1] = {
96-
type = 'delete',
97-
path = path,
98-
}
91+
m._callEvent('delete', path)
9992
end
10093
elseif flag & MODIFY ~= 0 then
101-
changes[#changes+1] = {
102-
type = 'change',
103-
path = path,
104-
}
94+
m._callEvent('change', path)
10595
end
10696
end
10797

108-
m._callEvent(changes)
10998
end
11099

111100
return m

script/provider/capability.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local completion = require 'provider.completion'
55
local define = require 'proto.define'
66

77
require 'provider.semantic-tokens'
8+
require 'provider.formatting'
89

910
local function toArray(map)
1011
local array = {}

script/provider/formatting.lua

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
local codeFormat = require 'code_format'
2+
local ws = require 'workspace'
3+
local furi = require 'file-uri'
4+
local fs = require 'bee.filesystem'
5+
local fw = require 'filewatch'
6+
local util = require 'utility'
7+
local diagnostics = require 'provider.diagnostic'
8+
9+
local loadedUris = {}
10+
11+
local updateType = {
12+
Created = 1,
13+
Changed = 2,
14+
Deleted = 3,
15+
}
16+
17+
fw.event(function (ev, path)
18+
if util.stringEndWith(path, '.editorconfig') then
19+
for uri, fsPath in pairs(loadedUris) do
20+
loadedUris[uri] = nil
21+
if fsPath ~= true then
22+
local status, err = codeFormat.update_config(updateType.Deleted, uri, fsPath:string())
23+
if not status and err then
24+
log.error(err)
25+
end
26+
end
27+
end
28+
for _, scp in ipairs(ws.folders) do
29+
diagnostics.diagnosticsScope(scp.uri)
30+
end
31+
end
32+
end)
33+
34+
local m = {}
35+
36+
---@param uri uri
37+
function m.updateConfig(uri)
38+
local currentUri = uri
39+
while true do
40+
currentUri = currentUri:match('^(.+)/[^/]*$')
41+
if not currentUri or loadedUris[currentUri] then
42+
return
43+
end
44+
loadedUris[currentUri] = true
45+
46+
local currentPath = furi.decode(currentUri)
47+
local editorConfigFSPath = fs.path(currentPath) / '.editorconfig'
48+
if fs.exists(editorConfigFSPath) then
49+
loadedUris[uri] = editorConfigFSPath
50+
local status, err = codeFormat.update_config(updateType.Created, currentUri, editorConfigFSPath:string())
51+
if not status and err then
52+
log.error(err)
53+
end
54+
end
55+
56+
if not ws.rootUri then
57+
return
58+
end
59+
60+
for _, scp in ipairs(ws.folders) do
61+
if scp.uri == currentUri then
62+
return
63+
end
64+
end
65+
end
66+
end
67+
68+
return m

script/provider/provider.lua

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,20 @@ function m.register(method)
6565
end
6666
end
6767

68-
filewatch.event(function (changes) ---@async
69-
for _, change in ipairs(changes) do
70-
if (CONFIGPATH and util.stringEndWith(change.path, CONFIGPATH)) then
71-
for _, scp in ipairs(workspace.folders) do
72-
local configPath = workspace.getAbsolutePath(scp.uri, CONFIGPATH)
73-
if change.path == configPath then
74-
updateConfig(scp.uri)
75-
end
68+
filewatch.event(function (ev, path) ---@async
69+
if (CONFIGPATH and util.stringEndWith(path, CONFIGPATH)) then
70+
for _, scp in ipairs(workspace.folders) do
71+
local configPath = workspace.getAbsolutePath(scp.uri, CONFIGPATH)
72+
if path == configPath then
73+
updateConfig(scp.uri)
7674
end
7775
end
78-
if util.stringEndWith(change.path, '.luarc.json') then
79-
for _, scp in ipairs(workspace.folders) do
80-
local rcPath = workspace.getAbsolutePath(scp.uri, '.luarc.json')
81-
if change.path == rcPath then
82-
updateConfig(scp.uri)
83-
end
76+
end
77+
if util.stringEndWith(path, '.luarc.json') then
78+
for _, scp in ipairs(workspace.folders) do
79+
local rcPath = workspace.getAbsolutePath(scp.uri, '.luarc.json')
80+
if path == rcPath then
81+
updateConfig(scp.uri)
8482
end
8583
end
8684
end
@@ -102,19 +100,6 @@ m.register 'initialize' {
102100
workspace.create(params.rootUri)
103101
end
104102

105-
if params.initializationOptions then
106-
if params.initializationOptions.editorConfigFiles then
107-
local codeFormat = require "code_format"
108-
for _, config in pairs(params.initializationOptions.editorConfigFiles) do
109-
local status, err = codeFormat.update_config(1, config.workspace, config.path)
110-
111-
if not status and err ~= nil then
112-
log.error(err)
113-
end
114-
end
115-
end
116-
end
117-
118103
return {
119104
capabilities = cap.getIniter(),
120105
serverInfo = {
@@ -938,6 +923,9 @@ m.register 'textDocument/formatting' {
938923
return nil
939924
end
940925

926+
local pformatting = require 'provider.formatting'
927+
pformatting.updateConfig(uri)
928+
941929
local core = require 'core.formatting'
942930
local edits = core(uri)
943931
if not edits or #edits == 0 then
@@ -965,6 +953,9 @@ m.register 'textDocument/rangeFormatting' {
965953
return nil
966954
end
967955

956+
local pformatting = require 'provider.formatting'
957+
pformatting.updateConfig(uri)
958+
968959
local core = require 'core.rangeformatting'
969960
local edits = core(uri, params.range)
970961
if not edits or #edits == 0 then
@@ -983,24 +974,6 @@ m.register 'textDocument/rangeFormatting' {
983974
end
984975
}
985976

986-
m.register 'config/editorconfig/update' {
987-
---@async
988-
function(params)
989-
local codeFormat = require "code_format"
990-
local status, err = codeFormat.update_config(params.type, params.source.workspace, params.source.path)
991-
992-
if not status and err ~= nil then
993-
log.error(err)
994-
return
995-
end
996-
997-
local diagnostic = require 'provider.diagnostic'
998-
for _, scp in ipairs(workspace.folders) do
999-
diagnostic.diagnosticsScope(scp.uri)
1000-
end
1001-
end
1002-
}
1003-
1004977
m.register 'textDocument/onTypeFormatting' {
1005978
abortByFileUpdate = true,
1006979
---@async

script/utility.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ local inf = 1 / 0
2323
local nan = 0 / 0
2424
local utf8 = utf8
2525
local error = error
26+
local upvalueid = debug.upvalueid
2627

2728
_ENV = nil
2829

@@ -721,4 +722,12 @@ function m.stringEndWith(str, tail)
721722
return str:sub(-#tail) == tail
722723
end
723724

725+
function m.defaultTable(default)
726+
return setmetatable({}, { __index = function (t, k)
727+
local v = default(k)
728+
t[k] = v
729+
return v
730+
end })
731+
end
732+
724733
return m

script/workspace/workspace.lua

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -500,44 +500,38 @@ config.watch(function (uri, key, value, oldValue)
500500
end
501501
end)
502502

503-
fw.event(function (changes) ---@async
504-
for _, change in ipairs(changes) do
505-
local path = change.path
506-
local uri = furi.encode(path)
507-
508-
---@async
509-
await.call(function ()
510-
if change.type == 'create' then
511-
log.debug('FileChangeType.Created', uri)
512-
m.awaitLoadFile(uri)
513-
elseif change.type == 'delete' then
514-
log.debug('FileChangeType.Deleted', uri)
515-
files.remove(uri)
516-
m.removeFile(uri)
517-
local childs = files.getChildFiles(uri)
518-
for _, curi in ipairs(childs) do
519-
log.debug('FileChangeType.Deleted.Child', curi)
520-
files.remove(curi)
521-
m.removeFile(uri)
522-
end
523-
elseif change.type == 'change' then
524-
if m.isValidLuaUri(uri) then
525-
-- 如果文件处于关闭状态,则立即更新;否则等待didChange协议来更新
526-
if not files.isOpen(uri) then
527-
files.setText(uri, pub.awaitTask('loadFile', furi.decode(uri)), false)
528-
end
529-
end
503+
fw.event(function (ev, path) ---@async
504+
local uri = furi.encode(path)
505+
506+
if ev == 'create' then
507+
log.debug('FileChangeType.Created', uri)
508+
m.awaitLoadFile(uri)
509+
elseif ev == 'delete' then
510+
log.debug('FileChangeType.Deleted', uri)
511+
files.remove(uri)
512+
m.removeFile(uri)
513+
local childs = files.getChildFiles(uri)
514+
for _, curi in ipairs(childs) do
515+
log.debug('FileChangeType.Deleted.Child', curi)
516+
files.remove(curi)
517+
m.removeFile(uri)
518+
end
519+
elseif ev == 'change' then
520+
if m.isValidLuaUri(uri) then
521+
-- 如果文件处于关闭状态,则立即更新;否则等待didChange协议来更新
522+
if not files.isOpen(uri) then
523+
files.setText(uri, pub.awaitTask('loadFile', furi.decode(uri)), false)
530524
end
531-
end)
525+
end
526+
end
532527

533-
local filename = fs.path(path):filename():string()
534-
-- 排除类文件发生更改需要重新扫描
535-
if filename == '.gitignore'
536-
or filename == '.gitmodules' then
537-
local scp = scope.getScope(uri)
538-
if scp.type ~= 'fallback' then
539-
m.reload(scp)
540-
end
528+
local filename = fs.path(path):filename():string()
529+
-- 排除类文件发生更改需要重新扫描
530+
if filename == '.gitignore'
531+
or filename == '.gitmodules' then
532+
local scp = scope.getScope(uri)
533+
if scp.type ~= 'fallback' then
534+
m.reload(scp)
541535
end
542536
end
543537
end)

0 commit comments

Comments
 (0)