11enum class ScanKind { Code, LineComment, BlockComment,
String };
14 ScanKind
kind = ScanKind::Code;
19void advance(ScanState& state, std::string_view
source,
size_t& i,
size_t end) {
23 if (
state.kind == ScanKind::LineComment) {
24 if (
value ==
'\n')
state.kind = ScanKind::Code;
28 if (
state.kind == ScanKind::BlockComment) {
29 if (
value ==
'*' && next ==
'/') {
30 state.kind = ScanKind::Code;
37 if (
state.kind == ScanKind::String) {
47 state.kind = ScanKind::Code;
54 if (
value ==
'/' && next ==
'/') {
55 state.kind = ScanKind::LineComment;
59 if (
value ==
'/' && next ==
'*') {
60 state.kind = ScanKind::BlockComment;
66 state.kind = ScanKind::String;
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');
81std::string rtrim(std::string_view
line) {
84 return std::string(
line.substr(0,
end));
87unsigned leadingCloses(std::string_view
line, ScanState state) {
90 while (i <
line.size() && (
line[i] ==
' ' ||
line[i] ==
'\t')) ++i;
91 while (i <
line.size()) {
92 const size_t before = i;
94 advance(state,
line, i,
line.size());
95 if (
kind != ScanKind::Code)
continue;
96 if (before >=
line.size())
break;
101 else if (!std::isspace(
static_cast<unsigned char>(
value)))
103 if (i == before) ++i;
108int braceDelta(std::string_view
line, ScanState& state) {
111 while (i <
line.size()) {
112 const size_t before = i;
114 advance(state,
line, i,
line.size());
115 if (
kind != ScanKind::Code)
continue;
116 if (before >=
line.size())
break;
121 if (i == before) ++i;
128 for (
size_t i = 0; i <
offset && i <
source.size(); ++i) {
145 if (newline == std::string_view::npos)
return source.size();
149 const size_t end = newline == std::string_view::npos ?
source.size() : newline;
154 return {{0, 0}, positionAtOffset(
source,
source.size())};
166 if (startLine > last) startLine = last;
167 if (endLineInclusive > last) endLineInclusive = last;
168 if (endLineInclusive < startLine) endLineInclusive = startLine;
171 if (endLineInclusive < last)
172 range.
end = {endLineInclusive + 1, 0};
178std::string
sliceLines(std::string_view
source,
size_t startLine,
size_t endLineInclusive) {
187 if (
range ==
nullptr) {
203 size_t lineBegin = 0;
207 for (
size_t i = 0; i <=
source.size(); ++i) {
209 const std::string_view
raw =
source.substr(lineBegin, i - lineBegin);
210 const std::string trimmed = rtrim(
raw);
212 while (content < trimmed.size() && (trimmed[content] ==
' ' || trimmed[content] ==
'\t')) ++content;
213 const bool blank = content == trimmed.size();
215 if (
state.kind == ScanKind::BlockComment)
result += trimmed;
216 }
else if (
state.kind == ScanKind::BlockComment) {
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);
224 indent += braceDelta(trimmed,
state);
225 if (indent < 0) indent = 0;
236 ScanKind
scan = ScanKind::Code;
238 std::vector<Open> stack;
239 std::vector<FoldingRange>
result;
242 for (
size_t i = 0; i <
source.size();) {
243 const size_t before = i;
251 if (
kind == ScanKind::Code &&
state.kind == ScanKind::BlockComment) {
252 stack.push_back({
line,
'*', ScanKind::BlockComment});
255 if (
kind == ScanKind::BlockComment &&
state.kind == ScanKind::Code) {
256 if (!stack.empty() && stack.back().kind ==
'*') {
257 const size_t start = stack.back().line;
263 if (
kind != ScanKind::Code)
continue;
265 else if ((
value ==
'}' ||
value ==
']') && !stack.empty() && stack.back().kind !=
'*') {
266 const size_t start = stack.back().line;
270 if (i == before) ++i;
std::array< float, 3 > position
const SquirrelValueOptions & options
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).
enum EVENGINE_API_FOUNDATION String
OT_STRING.