summaryrefslogtreecommitdiff
path: root/script/core
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-08-29 20:03:23 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-08-29 20:03:23 +0800
commit8da719f8c063b7b8f8a99d1e98201e44a13fab1f (patch)
treef4c1c6949a8f33a011321c3511d190e4cc6a6792 /script/core
parentc700a5fc23e00f95166e883764276a6f61803d4e (diff)
downloadlua-language-server-8da719f8c063b7b8f8a99d1e98201e44a13fab1f.zip
fix wront indent of VSCode
Diffstat (limited to 'script/core')
-rw-r--r--script/core/diagnostics/codestyle-check.lua6
-rw-r--r--script/core/formatting.lua5
-rw-r--r--script/core/rangeformatting.lua5
-rw-r--r--script/core/type-formatting.lua122
4 files changed, 129 insertions, 9 deletions
diff --git a/script/core/diagnostics/codestyle-check.lua b/script/core/diagnostics/codestyle-check.lua
index 25603b4b..6448979d 100644
--- a/script/core/diagnostics/codestyle-check.lua
+++ b/script/core/diagnostics/codestyle-check.lua
@@ -1,5 +1,4 @@
local files = require 'files'
-local codeFormat = require 'code_format'
local converter = require 'proto.converter'
local log = require 'log'
local pformatting = require 'provider.formatting'
@@ -12,6 +11,11 @@ return function(uri, callback)
return
end
+ local suc, codeFormat = pcall(require, 'code_format')
+ if not suc then
+ return
+ end
+
pformatting.updateConfig(uri)
local status, diagnosticInfos = codeFormat.diagnose_file(uri, text)
diff --git a/script/core/formatting.lua b/script/core/formatting.lua
index fb5ca9c7..de7f30ed 100644
--- a/script/core/formatting.lua
+++ b/script/core/formatting.lua
@@ -1,8 +1,11 @@
-local codeFormat = require("code_format")
local files = require("files")
local log = require("log")
return function(uri, options)
+ local suc, codeFormat = pcall(require, "code_format")
+ if not suc then
+ return
+ end
local text = files.getOriginText(uri)
local state = files.getState(uri)
if not state then
diff --git a/script/core/rangeformatting.lua b/script/core/rangeformatting.lua
index f64e9cda..04a61bd9 100644
--- a/script/core/rangeformatting.lua
+++ b/script/core/rangeformatting.lua
@@ -1,9 +1,12 @@
-local codeFormat = require("code_format")
local files = require("files")
local log = require("log")
local converter = require("proto.converter")
return function(uri, range, options)
+ local suc, codeFormat = pcall(require, "code_format")
+ if not suc then
+ return
+ end
local text = files.getOriginText(uri)
local status, formattedText, startLine, endLine = codeFormat.range_format(
uri, text, range.start.line, range["end"].line, options)
diff --git a/script/core/type-formatting.lua b/script/core/type-formatting.lua
index 90892604..d0d7462a 100644
--- a/script/core/type-formatting.lua
+++ b/script/core/type-formatting.lua
@@ -1,8 +1,9 @@
local files = require 'files'
local lookBackward = require 'core.look-backward'
-local guide = require "parser.guide"
-local codeFormat = require "code_format"
-local config = require "config"
+local guide = require 'parser.guide'
+local config = require 'config'
+local util = require 'utility'
+
local function insertIndentation(uri, position, edits)
local text = files.getText(uri)
@@ -88,10 +89,105 @@ local function checkSplitOneLine(results, uri, position, ch)
end
end
+local function getIndent(state, row)
+ local offset = state.lines[row]
+ local indent = state.lua:match('^[\t ]*', offset)
+ return indent
+end
+
+local function isInBlock(state, position)
+ local block = guide.eachSourceContain(state.ast, position, function(source)
+ if source.type == 'ifblock'
+ or source.type == 'elseifblock' then
+ if source.keyword[4] and source.keyword[4] <= position then
+ return true
+ end
+ end
+ if source.type == 'else' then
+ if source.keyword[2] and source.keyword[2] <= position then
+ return true
+ end
+ end
+ if source.type == 'while' then
+ if source.keyword[4] and source.keyword[4] <= position then
+ return true
+ end
+ end
+ if source.type == 'repeat' then
+ if source.keyword[2] and source.keyword[2] <= position then
+ return true
+ end
+ end
+ if source.type == 'loop' then
+ if source.keyword[4] and source.keyword[4] <= position then
+ return true
+ end
+ end
+ if source.type == 'in' then
+ if source.keyword[6] and source.keyword[6] <= position then
+ return true
+ end
+ end
+ if source.type == 'do' then
+ if source.keyword[2] and source.keyword[2] <= position then
+ return true
+ end
+ end
+ if source.type == 'function' then
+ if source.args and source.args.finish <= position then
+ return true
+ end
+ if not source.keyword[3] or source.keyword[3] >= position then
+ return true
+ end
+ end
+ end)
+ return block ~= nil
+end
+
+local function checkWrongIndentation(results, uri, position, ch)
+ if ch ~= '\n' then
+ return
+ end
+ local state = files.getState(uri)
+ if not state then
+ return
+ end
+ local row = guide.rowColOf(position)
+ if row <= 0 then
+ return
+ end
+ local myIndent = getIndent(state, row)
+ local lastIndent = getIndent(state, row - 1)
+ if #myIndent <= #lastIndent then
+ return
+ end
+ if not util.stringStartWith(myIndent, lastIndent) then
+ return
+ end
+ local lastOffset = lookBackward.findAnyOffset(state.lua, guide.positionToOffset(state, position) - 1)
+ if not lastOffset then
+ return
+ end
+ local lastPosition = guide.offsetToPosition(state, lastOffset)
+ if isInBlock(state, lastPosition) then
+ return
+ end
+ results[#results+1] = {
+ start = position - #myIndent + #lastIndent,
+ finish = position,
+ text = '',
+ }
+end
+
local function typeFormat(results, uri, position, ch, options)
if ch ~= '\n' then
return
end
+ local suc, codeFormat = pcall(require, "code_format")
+ if not suc then
+ return
+ end
local text = files.getOriginText(uri)
local state = files.getState(uri)
if not state then
@@ -120,9 +216,23 @@ return function (uri, position, ch, options)
local results = {}
-- split `function () $ end`
checkSplitOneLine(results, uri, position, ch)
- if #results == 0 then
- typeFormat(results, uri, position, ch, options)
+ if #results > 0 then
+ return results
end
- return results
+ checkWrongIndentation(results, uri, position, ch)
+ if #results > 0 then
+ return results
+ end
+
+ if TEST then
+ return nil
+ end
+
+ typeFormat(results, uri, position, ch, options)
+ if #results > 0 then
+ return results
+ end
+
+ return nil
end