diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-09-23 20:38:07 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-09-23 20:38:07 +0800 |
commit | 7ddc57e0f0a8b32d7d8bfa9228e856d897fc8de9 (patch) | |
tree | dce3b454d310f123631742aad664e370d36569a5 /script/core | |
parent | 5e6897bc1a25b936724af6eeb5fc463a8d395d1e (diff) | |
download | lua-language-server-7ddc57e0f0a8b32d7d8bfa9228e856d897fc8de9.zip |
update
Diffstat (limited to 'script/core')
-rw-r--r-- | script/core/code-action.lua | 1 | ||||
-rw-r--r-- | script/core/type-formatting.lua | 72 |
2 files changed, 38 insertions, 35 deletions
diff --git a/script/core/code-action.lua b/script/core/code-action.lua index c9f2ddd2..ff141bbf 100644 --- a/script/core/code-action.lua +++ b/script/core/code-action.lua @@ -7,7 +7,6 @@ local converter = require 'proto.converter' local function checkDisableByLuaDocExits(uri, row, mode, code) local state = files.getState(uri) - local text = files.getOriginText(uri) local lines = state.lines if state.ast.docs and lines then guide.eachSourceBetween(state.ast.docs, guide.positionOf(row, 0), guide.positionOf(row + 1, 0), function (doc) diff --git a/script/core/type-formatting.lua b/script/core/type-formatting.lua index e3494220..b946184b 100644 --- a/script/core/type-formatting.lua +++ b/script/core/type-formatting.lua @@ -2,85 +2,89 @@ local files = require 'files' local lookBackward = require 'core.look-backward' local guide = require "parser.guide" -local function insertIndentation(uri, offset, edits) - local text = files.getOriginText(uri) - local row = guide.rowColOf(offset) - local line = lines[row] - local indent = text:sub(line.start, line.finish):match '^%s*' +local function insertIndentation(uri, position, edits) + local text = files.getText(uri) + local state = files.getState(uri) + local row = guide.rowColOf(position) + local offset = state.lines[row] + local indent = text:match('^%s*', offset) for _, edit in ipairs(edits) do edit.text = edit.text:gsub('\n', '\n' .. indent) end end -local function findForward(text, offset, ...) - local pos = text:match('^[ \t]*()', offset) - if not pos then +local function findForward(uri, position, ...) + local text = files.getText(uri) + local state = files.getState(uri) + local offset = guide.positionToOffset(state, position) + local firstOffset = text:match('^[ \t]*()', offset + 1) + if not firstOffset then return nil end for _, symbol in ipairs { ... } do - if text:sub(pos, pos + #symbol - 1) == symbol then - return pos, symbol + if text:sub(firstOffset, firstOffset + #symbol - 1) == symbol then + return guide.offsetToPosition(state, firstOffset - 1), symbol end end return nil end -local function findBackward(text, offset, ...) - local pos = lookBackward.findAnyOffset(text, offset) +local function findBackward(uri, position, ...) + local text = files.getText(uri) + local state = files.getState(uri) + local offset = guide.positionToOffset(state, position) + local lastOffset = lookBackward.findAnyOffset(text, offset) for _, symbol in ipairs { ... } do - if text:sub(pos - #symbol + 1, pos) == symbol then - return pos - #symbol + 1, symbol + if text:sub(lastOffset - #symbol + 1, lastOffset) == symbol then + return guide.offsetToPosition(state, lastOffset) end end return nil end -local function checkSplitOneLine(results, uri, offset, ch) - do return end +local function checkSplitOneLine(results, uri, position, ch) if ch ~= '\n' then return end - local text = files.getOriginText(uri) - local fOffset, fSymbol = findForward(text, offset + 1, 'end', '}') - if not fOffset then + local fPosition, fSymbol = findForward(uri, position, 'end', '}') + if not fPosition then return end - local bOffset, bSymbol = findBackward(text, offset, 'then', 'do', ')', '{') - if not bOffset then + local bPosition = findBackward(uri, position, 'then', 'do', ')', '{') + if not bPosition then return end local edits = {} edits[#edits+1] = { - start = bOffset + #bSymbol, - finish = offset, + start = bPosition, + finish = position, text = '\n\t', } edits[#edits+1] = { - start = offset + 1, - finish = fOffset + #fSymbol - 1, - text = '' + start = position, + finish = fPosition + 1, + text = '', } edits[#edits+1] = { - start = fOffset + #fSymbol, - finish = fOffset + #fSymbol - 1, - text = '\n' .. fSymbol + start = fPosition + 1, + finish = fPosition + 1, + text = '\n' .. fSymbol:sub(1, 1) } - insertIndentation(uri, bOffset, edits) + insertIndentation(uri, bPosition, edits) for _, edit in ipairs(edits) do results[#results+1] = edit end end -return function (uri, offset, ch) +return function (uri, position, ch) local ast = files.getState(uri) - local text = files.getOriginText(uri) - if not ast or not text then + if not ast then return nil end local results = {} -- split `function () $ end` - checkSplitOneLine(results, uri, offset, ch) + checkSplitOneLine(results, uri, position, ch) return results end |