|
1 | 1 | local files = require 'files' |
2 | 2 | local lookBackward = require 'core.look-backward' |
3 | | -local guide = require "parser.guide" |
4 | | -local codeFormat = require "code_format" |
5 | | -local config = require "config" |
| 3 | +local guide = require 'parser.guide' |
| 4 | +local config = require 'config' |
| 5 | +local util = require 'utility' |
| 6 | + |
6 | 7 |
|
7 | 8 | local function insertIndentation(uri, position, edits) |
8 | 9 | local text = files.getText(uri) |
@@ -88,10 +89,105 @@ local function checkSplitOneLine(results, uri, position, ch) |
88 | 89 | end |
89 | 90 | end |
90 | 91 |
|
| 92 | +local function getIndent(state, row) |
| 93 | + local offset = state.lines[row] |
| 94 | + local indent = state.lua:match('^[\t ]*', offset) |
| 95 | + return indent |
| 96 | +end |
| 97 | + |
| 98 | +local function isInBlock(state, position) |
| 99 | + local block = guide.eachSourceContain(state.ast, position, function(source) |
| 100 | + if source.type == 'ifblock' |
| 101 | + or source.type == 'elseifblock' then |
| 102 | + if source.keyword[4] and source.keyword[4] <= position then |
| 103 | + return true |
| 104 | + end |
| 105 | + end |
| 106 | + if source.type == 'else' then |
| 107 | + if source.keyword[2] and source.keyword[2] <= position then |
| 108 | + return true |
| 109 | + end |
| 110 | + end |
| 111 | + if source.type == 'while' then |
| 112 | + if source.keyword[4] and source.keyword[4] <= position then |
| 113 | + return true |
| 114 | + end |
| 115 | + end |
| 116 | + if source.type == 'repeat' then |
| 117 | + if source.keyword[2] and source.keyword[2] <= position then |
| 118 | + return true |
| 119 | + end |
| 120 | + end |
| 121 | + if source.type == 'loop' then |
| 122 | + if source.keyword[4] and source.keyword[4] <= position then |
| 123 | + return true |
| 124 | + end |
| 125 | + end |
| 126 | + if source.type == 'in' then |
| 127 | + if source.keyword[6] and source.keyword[6] <= position then |
| 128 | + return true |
| 129 | + end |
| 130 | + end |
| 131 | + if source.type == 'do' then |
| 132 | + if source.keyword[2] and source.keyword[2] <= position then |
| 133 | + return true |
| 134 | + end |
| 135 | + end |
| 136 | + if source.type == 'function' then |
| 137 | + if source.args and source.args.finish <= position then |
| 138 | + return true |
| 139 | + end |
| 140 | + if not source.keyword[3] or source.keyword[3] >= position then |
| 141 | + return true |
| 142 | + end |
| 143 | + end |
| 144 | + end) |
| 145 | + return block ~= nil |
| 146 | +end |
| 147 | + |
| 148 | +local function checkWrongIndentation(results, uri, position, ch) |
| 149 | + if ch ~= '\n' then |
| 150 | + return |
| 151 | + end |
| 152 | + local state = files.getState(uri) |
| 153 | + if not state then |
| 154 | + return |
| 155 | + end |
| 156 | + local row = guide.rowColOf(position) |
| 157 | + if row <= 0 then |
| 158 | + return |
| 159 | + end |
| 160 | + local myIndent = getIndent(state, row) |
| 161 | + local lastIndent = getIndent(state, row - 1) |
| 162 | + if #myIndent <= #lastIndent then |
| 163 | + return |
| 164 | + end |
| 165 | + if not util.stringStartWith(myIndent, lastIndent) then |
| 166 | + return |
| 167 | + end |
| 168 | + local lastOffset = lookBackward.findAnyOffset(state.lua, guide.positionToOffset(state, position) - 1) |
| 169 | + if not lastOffset then |
| 170 | + return |
| 171 | + end |
| 172 | + local lastPosition = guide.offsetToPosition(state, lastOffset) |
| 173 | + if isInBlock(state, lastPosition) then |
| 174 | + return |
| 175 | + end |
| 176 | + results[#results+1] = { |
| 177 | + start = position - #myIndent + #lastIndent, |
| 178 | + finish = position, |
| 179 | + text = '', |
| 180 | + } |
| 181 | +end |
| 182 | + |
91 | 183 | local function typeFormat(results, uri, position, ch, options) |
92 | 184 | if ch ~= '\n' then |
93 | 185 | return |
94 | 186 | end |
| 187 | + local suc, codeFormat = pcall(require, "code_format") |
| 188 | + if not suc then |
| 189 | + return |
| 190 | + end |
95 | 191 | local text = files.getOriginText(uri) |
96 | 192 | local state = files.getState(uri) |
97 | 193 | if not state then |
@@ -120,9 +216,23 @@ return function (uri, position, ch, options) |
120 | 216 | local results = {} |
121 | 217 | -- split `function () $ end` |
122 | 218 | checkSplitOneLine(results, uri, position, ch) |
123 | | - if #results == 0 then |
124 | | - typeFormat(results, uri, position, ch, options) |
| 219 | + if #results > 0 then |
| 220 | + return results |
125 | 221 | end |
126 | 222 |
|
127 | | - return results |
| 223 | + checkWrongIndentation(results, uri, position, ch) |
| 224 | + if #results > 0 then |
| 225 | + return results |
| 226 | + end |
| 227 | + |
| 228 | + if TEST then |
| 229 | + return nil |
| 230 | + end |
| 231 | + |
| 232 | + typeFormat(results, uri, position, ch, options) |
| 233 | + if #results > 0 then |
| 234 | + return results |
| 235 | + end |
| 236 | + |
| 237 | + return nil |
128 | 238 | end |
0 commit comments