载入中...
搜索中...
未找到
LanguageText.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cctype>
5#include <utility>
6#include <vector>
7
8namespace eve::dev::lsp {
9namespace {
10
11enum class ScanKind { Code, LineComment, BlockComment, String };
12
13struct ScanState {
14 ScanKind kind = ScanKind::Code;
15 char quote = 0;
16 bool verbatim = false;
17};
18
19void advance(ScanState& state, std::string_view source, size_t& i, size_t end) {
20 if (i >= end) return;
21 const char value = source[i];
22 const char next = i + 1 < end ? source[i + 1] : 0;
23 if (state.kind == ScanKind::LineComment) {
24 if (value == '\n') state.kind = ScanKind::Code;
25 ++i;
26 return;
27 }
28 if (state.kind == ScanKind::BlockComment) {
29 if (value == '*' && next == '/') {
30 state.kind = ScanKind::Code;
31 i += 2;
32 return;
33 }
34 ++i;
35 return;
36 }
37 if (state.kind == ScanKind::String) {
38 if (!state.verbatim && value == '\\' && i + 1 < end) {
39 i += 2;
40 return;
41 }
42 if (state.verbatim && value == state.quote && next == state.quote) {
43 i += 2;
44 return;
45 }
46 if (value == state.quote) {
47 state.kind = ScanKind::Code;
48 ++i;
49 return;
50 }
51 ++i;
52 return;
53 }
54 if (value == '/' && next == '/') {
55 state.kind = ScanKind::LineComment;
56 i += 2;
57 return;
58 }
59 if (value == '/' && next == '*') {
60 state.kind = ScanKind::BlockComment;
61 i += 2;
62 return;
63 }
64 const bool verbatim = value == '@' && (next == '"' || next == '\'');
65 if (value == '"' || value == '\'' || verbatim) {
66 state.kind = ScanKind::String;
67 state.quote = verbatim ? next : value;
68 state.verbatim = verbatim;
69 i += verbatim ? 2 : 1;
70 return;
71 }
72 ++i;
73}
74
75std::string indentText(unsigned depth, const FormatOptions& options) {
76 if (depth == 0) return {};
77 if (options.insertSpaces) return std::string(static_cast<size_t>(depth) * options.tabSize, ' ');
78 return std::string(depth, '\t');
79}
80
81std::string rtrim(std::string_view line) {
82 size_t end = line.size();
83 while (end > 0 && (line[end - 1] == ' ' || line[end - 1] == '\t' || line[end - 1] == '\r')) --end;
84 return std::string(line.substr(0, end));
85}
86
87unsigned leadingCloses(std::string_view line, ScanState state) {
88 unsigned closes = 0;
89 size_t i = 0;
90 while (i < line.size() && (line[i] == ' ' || line[i] == '\t')) ++i;
91 while (i < line.size()) {
92 const size_t before = i;
93 const auto kind = state.kind;
94 advance(state, line, i, line.size());
95 if (kind != ScanKind::Code) continue;
96 if (before >= line.size()) break;
97 const char value = line[before];
98 if (value == '}' || value == ']') ++closes;
99 else if (value == '{' || value == '[')
100 break;
101 else if (!std::isspace(static_cast<unsigned char>(value)))
102 break;
103 if (i == before) ++i;
104 }
105 return closes;
106}
107
108int braceDelta(std::string_view line, ScanState& state) {
109 int delta = 0;
110 size_t i = 0;
111 while (i < line.size()) {
112 const size_t before = i;
113 const auto kind = state.kind;
114 advance(state, line, i, line.size());
115 if (kind != ScanKind::Code) continue;
116 if (before >= line.size()) break;
117 const char value = line[before];
118 if (value == '{' || value == '[') ++delta;
119 else if (value == '}' || value == ']')
120 --delta;
121 if (i == before) ++i;
122 }
123 return delta;
124}
125
126Position positionAtOffset(std::string_view source, size_t offset) {
128 for (size_t i = 0; i < offset && i < source.size(); ++i) {
129 if (source[i] == '\n') {
130 ++position.line;
131 position.character = 0;
132 } else {
133 ++position.character;
134 }
135 }
136 return position;
137}
138
139} // namespace
140
141size_t offsetAt(std::string_view source, Position position) noexcept {
142 size_t offset = 0;
143 for (size_t line = 0; line < position.line && offset < source.size(); ++line) {
144 const size_t newline = source.find('\n', offset);
145 if (newline == std::string_view::npos) return source.size();
146 offset = newline + 1;
147 }
148 const size_t newline = source.find('\n', offset);
149 const size_t end = newline == std::string_view::npos ? source.size() : newline;
150 return offset + std::min(position.character, end - offset);
151}
152
153Range documentRange(std::string_view source) noexcept {
154 return {{0, 0}, positionAtOffset(source, source.size())};
155}
156
157size_t lastLineIndex(std::string_view source) noexcept {
158 size_t line = 0;
159 for (const char value : source)
160 if (value == '\n') ++line;
161 return line;
162}
163
164Range coveringLines(std::string_view source, size_t startLine, size_t endLineInclusive) noexcept {
165 const size_t last = lastLineIndex(source);
166 if (startLine > last) startLine = last;
167 if (endLineInclusive > last) endLineInclusive = last;
168 if (endLineInclusive < startLine) endLineInclusive = startLine;
169 Range range;
170 range.start = {startLine, 0};
171 if (endLineInclusive < last)
172 range.end = {endLineInclusive + 1, 0};
173 else
174 range.end = positionAtOffset(source, source.size());
175 return range;
176}
177
178std::string sliceLines(std::string_view source, size_t startLine, size_t endLineInclusive) {
179 const Range range = coveringLines(source, startLine, endLineInclusive);
180 const size_t begin = offsetAt(source, range.start);
181 const size_t end = offsetAt(source, range.end);
182 if (begin > end) return {};
183 return std::string(source.substr(begin, end - begin));
184}
185
186bool applyIncremental(std::string& source, const Range* range, std::string_view text) {
187 if (range == nullptr) {
188 source = std::string(text);
189 return true;
190 }
191 const size_t begin = offsetAt(source, range->start);
192 const size_t end = offsetAt(source, range->end);
193 if (begin > end || end > source.size()) return false;
194 source.replace(begin, end - begin, text);
195 return true;
196}
197
198std::string formatEveScript(std::string_view source, const FormatOptions& options) {
199 std::string result;
200 result.reserve(source.size());
201 ScanState state;
202 int indent = 0;
203 size_t lineBegin = 0;
204 const unsigned tab = options.tabSize == 0 ? 4 : options.tabSize;
205 FormatOptions normalized = options;
206 normalized.tabSize = tab;
207 for (size_t i = 0; i <= source.size(); ++i) {
208 if (i < source.size() && source[i] != '\n') continue;
209 const std::string_view raw = source.substr(lineBegin, i - lineBegin);
210 const std::string trimmed = rtrim(raw);
211 size_t content = 0;
212 while (content < trimmed.size() && (trimmed[content] == ' ' || trimmed[content] == '\t')) ++content;
213 const bool blank = content == trimmed.size();
214 if (blank) {
215 if (state.kind == ScanKind::BlockComment) result += trimmed;
216 } else if (state.kind == ScanKind::BlockComment) {
217 result += trimmed;
218 } else {
219 const unsigned closes = leadingCloses(trimmed, state);
220 const int display = indent - static_cast<int>(closes);
221 result += indentText(display < 0 ? 0 : static_cast<unsigned>(display), normalized);
222 result += trimmed.substr(content);
223 }
224 indent += braceDelta(trimmed, state);
225 if (indent < 0) indent = 0;
226 if (i < source.size()) result += '\n';
227 lineBegin = i + 1;
228 }
229 return result;
230}
231
232std::vector<FoldingRange> foldingRanges(std::string_view source) {
233 struct Open {
234 size_t line = 0;
235 char kind = '{';
236 ScanKind scan = ScanKind::Code;
237 };
238 std::vector<Open> stack;
239 std::vector<FoldingRange> result;
240 ScanState state;
241 size_t line = 0;
242 for (size_t i = 0; i < source.size();) {
243 const size_t before = i;
244 const auto kind = state.kind;
245 const char value = source[i];
246 advance(state, source, i, source.size());
247 if (value == '\n') {
248 ++line;
249 continue;
250 }
251 if (kind == ScanKind::Code && state.kind == ScanKind::BlockComment) {
252 stack.push_back({line, '*', ScanKind::BlockComment});
253 continue;
254 }
255 if (kind == ScanKind::BlockComment && state.kind == ScanKind::Code) {
256 if (!stack.empty() && stack.back().kind == '*') {
257 const size_t start = stack.back().line;
258 stack.pop_back();
259 if (line > start) result.push_back({start, line, "comment"});
260 }
261 continue;
262 }
263 if (kind != ScanKind::Code) continue;
264 if (value == '{' || value == '[') stack.push_back({line, value, ScanKind::Code});
265 else if ((value == '}' || value == ']') && !stack.empty() && stack.back().kind != '*') {
266 const size_t start = stack.back().line;
267 stack.pop_back();
268 if (line > start) result.push_back({start, line, "region"});
269 }
270 if (i == before) ++i;
271 }
272 return result;
273}
274
275} // namespace eve::dev::lsp
Duration start
double raw
size_t offset
Range range
bool verbatim
char quote
std::array< float, 3 > position
Poco::JSON::Object::Ptr result
Object::Ptr value
unsigned line
std::string text
std::string_view source
size_t begin
size_t end
std::string kind
const SquirrelValueOptions & options
std::uint32_t depth
size_t offsetAt(std::string_view source, Position position) noexcept
Converts an LSP position into a UTF-8 byte offset (same contract as the rest of the server).
size_t lastLineIndex(std::string_view source) noexcept
Last 0-based line index (empty source is line 0).
std::string sliceLines(std::string_view source, size_t startLine, size_t endLineInclusive)
Source bytes for coveringLines of the same line span.
bool applyIncremental(std::string &source, const Range *range, std::string_view text)
Applies one LSP content change.
std::string formatEveScript(std::string_view source, const FormatOptions &options)
Indents EveScript by braces/brackets and trims trailing whitespace.
Range coveringLines(std::string_view source, size_t startLine, size_t endLineInclusive) noexcept
Half-open LSP range covering startLine through endLine inclusive. Ends at the start of the following ...
Range documentRange(std::string_view source) noexcept
Full-document range covering every character in source.
std::vector< FoldingRange > foldingRanges(std::string_view source)
Brace, bracket, and block-comment folds that span more than one line.
Files scan(const std::filesystem::path &root, const Files &previous, Object::Ptr stats)
Bounded filesystem scan. Parses only changed files; symlinks are excluded.
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
Definition HexMetrics.h:76
enum EVENGINE_API_FOUNDATION String
OT_STRING.
Definition Runtime.h:119
Editor formatting preferences from LSP FormattingOptions.
Position public API.
Range public API.