summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/core/command/autoRequire.lua23
-rw-r--r--test/command/auto-require.lua19
2 files changed, 26 insertions, 16 deletions
diff --git a/script/core/command/autoRequire.lua b/script/core/command/autoRequire.lua
index e784a827..d711fb16 100644
--- a/script/core/command/autoRequire.lua
+++ b/script/core/command/autoRequire.lua
@@ -4,19 +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 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
@@ -32,7 +35,7 @@ local function findInsertOffset(uri)
end
end
end
- return 1, fmt
+ return 0, fmt
end
local function askAutoRequire(visiblePaths)
@@ -69,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)
@@ -87,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,
}
})
@@ -120,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
diff --git a/test/command/auto-require.lua b/test/command/auto-require.lua
index 94bb5069..a52662fb 100644
--- a/test/command/auto-require.lua
+++ b/test/command/auto-require.lua
@@ -3,7 +3,7 @@ local files = require 'files'
local autoRequire = require 'core.command.autoRequire'
local client = require 'client'
-local findInsertOffset = util.getUpvalue(autoRequire, 'findInsertOffset')
+local findInsertRow = util.getUpvalue(autoRequire, 'findInsertRow')
local applyAutoRequire = util.getUpvalue(autoRequire, 'applyAutoRequire')
local originEditText = client.editText
@@ -19,18 +19,25 @@ function TEST(text)
files.removeAll()
files.setText('', text)
EditResult = nil
- local offset, fmt = findInsertOffset('')
- applyAutoRequire('', offset, name, name, fmt)
+ local row, fmt = findInsertRow('')
+ applyAutoRequire('', row, name, name, fmt)
assert(util.equal(EditResult, expect))
end
end
end
--- TODO change to position
TEST '' 'test' {
start = 0,
- finish = -1,
- text = '\nlocal test = require "test"\n'
+ finish = 0,
+ text = 'local test = require "test"\n'
+}
+
+TEST [[
+local aaaaaa = require 'aaa'
+]] 'test' {
+ start = 10000,
+ finish = 10000,
+ text = 'local test = require \'test\'\n'
}
client.editText = originEditText