diff options
| author | Yong He <yonghe@outlook.com> | 2023-10-05 17:05:20 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-05 17:05:20 -0700 |
| commit | 4547125ce945140dc10542e9606b225dd06159b8 (patch) | |
| tree | 0c7df4b3d97143eb2dc78c68593269dac5cb5abf /source/slang/slang-language-server-auto-format.cpp | |
| parent | 441e13e13f30b96eb04c05725ad7fe1983c92f53 (diff) | |
Add intellisense support for spirv_asm blocks. (#3264)
Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'source/slang/slang-language-server-auto-format.cpp')
| -rw-r--r-- | source/slang/slang-language-server-auto-format.cpp | 86 |
1 files changed, 85 insertions, 1 deletions
diff --git a/source/slang/slang-language-server-auto-format.cpp b/source/slang/slang-language-server-auto-format.cpp index 5b94712a0..154122769 100644 --- a/source/slang/slang-language-server-auto-format.cpp +++ b/source/slang/slang-language-server-auto-format.cpp @@ -58,6 +58,80 @@ String findClangFormatTool() return String(); } +List<TextRange> extractFormattingExclusionRanges(UnownedStringSlice text) +{ + List<TextRange> resultRanges; + // Search the document and find all the spirv-asm blocks to exclude them from formatting. + Lexer lexer; + SourceManager manager; + manager.initialize(nullptr, nullptr); + auto sourceFile = manager.createSourceFileWithString(PathInfo(), text); + auto sourceView = manager.createSourceView(sourceFile, nullptr, SourceLoc()); + DiagnosticSink sink; + RootNamePool rootPool; + NamePool namePool; + namePool.setRootNamePool(&rootPool); + MemoryArena memory; + memory.init(1 << 16); + lexer.initialize(sourceView, &sink, &namePool, &memory); + for (;;) + { + auto headerToken = lexer.lexToken(); + if (headerToken.type == TokenType::EndOfFile) + break; + if (headerToken.type != TokenType::Identifier || headerToken.getContent() != toSlice("spirv_asm")) + { + continue; + } + + // We have found a spirv-asm block, now find the end of it. + int braceCounter = 0; + Token endToken; + Token startToken; + for (;;) + { + auto token = lexer.lexToken(); + if (token.type == TokenType::EndOfFile) + break; + switch (token.type) + { + case TokenType::RBrace: + braceCounter--; + if (braceCounter == 0) + { + endToken = token; + goto breakLabel; + } + case TokenType::LBrace: + { + if (braceCounter == 0) + startToken = token; + braceCounter++; + } + break; + case TokenType::WhiteSpace: + case TokenType::LineComment: + case TokenType::BlockComment: + case TokenType::NewLine: + break; + default: + if (braceCounter == 0) + goto breakLabel; + break; + } + } + breakLabel:; + if (endToken.type == TokenType::RBrace) + { + TextRange range; + range.offsetStart = startToken.getLoc().getRaw(); + range.offsetEnd = endToken.getLoc().getRaw(); + resultRanges.add(range); + } + } + return resultRanges; +} + void translateXmlEscape(StringBuilder& sb, UnownedStringSlice text) { if (text.getLength() == 0) @@ -152,7 +226,7 @@ bool shouldUseFallbackStyle(const FormatOptions& options) return true; } -List<Edit> formatSource(UnownedStringSlice text, Index lineStart, Index lineEnd, Index cursorOffset, const FormatOptions& options) +List<Edit> formatSource(UnownedStringSlice text, Index lineStart, Index lineEnd, Index cursorOffset, const List<TextRange>& exclusionRanges, const FormatOptions& options) { List<Edit> edits; @@ -232,6 +306,16 @@ List<Edit> formatSource(UnownedStringSlice text, Index lineStart, Index lineEnd, pos = line.indexOf(lengthStr); if (pos == -1) continue; + Index exclusionRangeId = exclusionRanges.binarySearch(edt.offset, [](TextRange range, Index offset) + { + if (range.offsetEnd <= offset) + return -1; + if (range.offsetStart > offset) + return 1; + return 0; + }); + if (exclusionRangeId != -1) + continue; pos += lengthStr.getLength(); if (pos < line.getLength() && line[pos] == '\'') pos++; |
