diff options
author | Arcanox <arcanox@arcanox.me> | 2021-09-25 18:00:15 -0500 |
---|---|---|
committer | Arcanox <arcanox@arcanox.me> | 2021-09-25 18:00:15 -0500 |
commit | dc685d1addad2f2e57f55a20bb6cca79c222c130 (patch) | |
tree | abb200fd7f217460a4543cb6f32af7ebac308bc0 /script/core/command/autoRequire.lua | |
parent | a465b35d5eefc11c1daf3c29b41ce95ee098a782 (diff) | |
parent | 1f0a2d0e9283a4cb7f7b3fc72258eb1c5ba4e5dd (diff) | |
download | lua-language-server-dc685d1addad2f2e57f55a20bb6cca79c222c130.zip |
Merge branch 'master' into improve-semantic-highlighting
# Conflicts:
# script/core/semantic-tokens.lua
Diffstat (limited to 'script/core/command/autoRequire.lua')
-rw-r--r-- | script/core/command/autoRequire.lua | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/script/core/command/autoRequire.lua b/script/core/command/autoRequire.lua index 2cb6a8f8..b485fcb1 100644 --- a/script/core/command/autoRequire.lua +++ b/script/core/command/autoRequire.lua @@ -4,21 +4,32 @@ local config = require 'config' local rpath = require 'workspace.require-path' local client = require 'client' local lang = require 'language' +local guide = require 'parser.guide' -local function findInsertOffset(uri) - local lines = files.getLines(uri) +local function findInsertRow(uri) local text = files.getText(uri) + local state = files.getState(uri) + local lines = state.lines local fmt = { pair = false, quot = '"', col = nil, } - for i = 1, #lines do + local hasFounded + for i = 0, #lines do local ln = lines[i] - local lnText = text:sub(ln.start, ln.finish) + local lnText = text:match('[^\r\n]*', ln) if not lnText:find('require', 1, true) then - return ln.start, fmt + if hasFounded then + return i, fmt + end + if not lnText:match '^local%s' + and not lnText:match '^%s*$' + and not lnText:match '^%-%-' then + return i, fmt + end else + hasFounded = true local lpPos = lnText:find '%(' if lpPos then fmt.pair = true @@ -33,7 +44,7 @@ local function findInsertOffset(uri) end end end - return 1, fmt + return 0, fmt end local function askAutoRequire(visiblePaths) @@ -70,7 +81,7 @@ local function askAutoRequire(visiblePaths) return nameMap[result] end -local function applyAutoRequire(uri, offset, name, result, fmt) +local function applyAutoRequire(uri, row, name, result, fmt) local quotedResult = ('%q'):format(result) if fmt.quot == "'" then quotedResult = ([['%s']]):format(quotedResult:sub(2, -2) @@ -88,11 +99,11 @@ local function applyAutoRequire(uri, offset, name, result, fmt) if fmt.col and fmt.col > #text then sp = (' '):rep(fmt.col - #text - 1) end - text = ('\nlocal %s%s= require%s\n'):format(name, sp, quotedResult) + text = ('local %s%s= require%s\n'):format(name, sp, quotedResult) client.editText(uri, { { - start = offset, - finish = offset - 1, + start = guide.positionOf(row, 0), + finish = guide.positionOf(row, 0), text = text, } }) @@ -121,6 +132,6 @@ return function (data) return end - local offset, fmt = findInsertOffset(uri) + local offset, fmt = findInsertRow(uri) applyAutoRequire(uri, offset, name, result, fmt) end |