From f73fe1369a79344394bf258df1782b9aafe4eff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 12 Oct 2021 17:09:12 +0800 Subject: fix #729 --- changelog.md | 1 + script/core/command/autoRequire.lua | 16 ++++++++++++++++ test/command/auto-require.lua | 30 ++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/changelog.md b/changelog.md index b8ad4ac9..08e3cae5 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,7 @@ * `FIX` [#718](https://github.com/sumneko/lua-language-server/issues/718) * `FIX` [#719](https://github.com/sumneko/lua-language-server/issues/719) * `FIX` [#725](https://github.com/sumneko/lua-language-server/issues/725) +* `FIX` [#729](https://github.com/sumneko/lua-language-server/issues/729) * `FIX` runtime errors ## 2.4.2 diff --git a/script/core/command/autoRequire.lua b/script/core/command/autoRequire.lua index b485fcb1..56096c55 100644 --- a/script/core/command/autoRequire.lua +++ b/script/core/command/autoRequire.lua @@ -6,6 +6,18 @@ local client = require 'client' local lang = require 'language' local guide = require 'parser.guide' +local function inComment(state, pos) + for _, comm in ipairs(state.comms) do + if comm.start <= pos and comm.finish >= pos then + return true + end + if comm.start > pos then + break + end + end + return false +end + local function findInsertRow(uri) local text = files.getText(uri) local state = files.getState(uri) @@ -17,6 +29,9 @@ local function findInsertRow(uri) } local hasFounded for i = 0, #lines do + if inComment(state, guide.positionOf(i, 0)) then + goto CONTINUE + end local ln = lines[i] local lnText = text:match('[^\r\n]*', ln) if not lnText:find('require', 1, true) then @@ -43,6 +58,7 @@ local function findInsertRow(uri) fmt.col = eqPos end end + ::CONTINUE:: end return 0, fmt end diff --git a/test/command/auto-require.lua b/test/command/auto-require.lua index a52662fb..c2c0935f 100644 --- a/test/command/auto-require.lua +++ b/test/command/auto-require.lua @@ -40,4 +40,34 @@ local aaaaaa = require 'aaa' text = 'local test = require \'test\'\n' } +TEST [[ +local DEBUG = true +local aaaaaa = require 'aaa' +]] 'test' { + start = 20000, + finish = 20000, + text = 'local test = require \'test\'\n' +} + +TEST [[ +-- comment +-- comment +local aaaaaa = require 'aaa' +]] 'test' { + start = 30000, + finish = 30000, + text = 'local test = require \'test\'\n' +} + +TEST [[ +--[=[ +comment chunk +]=] +local aaaaaa = require 'aaa' +]] 'test' { + start = 40000, + finish = 40000, + text = 'local test = require \'test\'\n' +} + client.editText = originEditText -- cgit v1.2.3