summaryrefslogtreecommitdiff
path: root/script/core/command/autoRequire.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-09-24 15:08:02 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-09-24 15:08:02 +0800
commit4b085b8aea5f33ec114baa31d2b9d72341383c32 (patch)
treefe35a326408e762711a31d3e803464f0c1a8468d /script/core/command/autoRequire.lua
parent0c8c6bbf23082d0b858646846a47a3001f718ae2 (diff)
parent35ce57976db3b4c42193279dd55972ea013fecad (diff)
downloadlua-language-server-4b085b8aea5f33ec114baa31d2b9d72341383c32.zip
Merge branch 'newparser'
Diffstat (limited to 'script/core/command/autoRequire.lua')
-rw-r--r--script/core/command/autoRequire.lua24
1 files changed, 13 insertions, 11 deletions
diff --git a/script/core/command/autoRequire.lua b/script/core/command/autoRequire.lua
index 2cb6a8f8..d711fb16 100644
--- a/script/core/command/autoRequire.lua
+++ b/script/core/command/autoRequire.lua
@@ -4,20 +4,22 @@ 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
+ 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
+ return i, fmt
else
local lpPos = lnText:find '%('
if lpPos then
@@ -33,7 +35,7 @@ local function findInsertOffset(uri)
end
end
end
- return 1, fmt
+ return 0, fmt
end
local function askAutoRequire(visiblePaths)
@@ -70,7 +72,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 +90,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 +123,6 @@ return function (data)
return
end
- local offset, fmt = findInsertOffset(uri)
+ local offset, fmt = findInsertRow(uri)
applyAutoRequire(uri, offset, name, result, fmt)
end