diff options
| author | Yong He <yonghe@outlook.com> | 2022-06-07 14:57:35 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-07 14:57:35 -0700 |
| commit | 01d0154ae90f5c587321d39b8fd8f82e2764f360 (patch) | |
| tree | 3f441db0781e9fd4ebcccf4cbf7d743f4910c72d /source | |
| parent | 0c64995ea28febcc7d38e1519da8d93391ce2e7d (diff) | |
Code review fixes for language server. (#2265)
* Code review fixes for language server.
* Fix clang error.
* update solution file
Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'source')
| -rw-r--r-- | source/core/slang-range.h | 29 | ||||
| -rw-r--r-- | source/slang/slang-ast-print.cpp | 4 | ||||
| -rw-r--r-- | source/slang/slang-ast-print.h | 3 | ||||
| -rw-r--r-- | source/slang/slang-language-server-semantic-tokens.cpp | 6 | ||||
| -rw-r--r-- | source/slang/slang-language-server-semantic-tokens.h | 4 | ||||
| -rw-r--r-- | source/slang/slang-language-server.cpp | 7 | ||||
| -rw-r--r-- | source/slang/slang-workspace-version.cpp | 57 | ||||
| -rw-r--r-- | source/slang/slang-workspace-version.h | 2 |
8 files changed, 59 insertions, 53 deletions
diff --git a/source/core/slang-range.h b/source/core/slang-range.h new file mode 100644 index 000000000..6e4419ce0 --- /dev/null +++ b/source/core/slang-range.h @@ -0,0 +1,29 @@ +#ifndef SLANG_CORE_RANGE_H +#define SLANG_CORE_RANGE_H + +namespace Slang +{ + template<typename T> + struct Range + { + T begin = 0; + T end = 0; + + bool inRange(T val) const + { + return val >= begin && val < end; + } + }; + + template <typename T> + Range<T> makeRange(T begin, T end) + { + Range<T> result; + result.begin = begin; + result.end = end; + return result; + } + +} + +#endif //SLANG_CORE_RANGE_H diff --git a/source/slang/slang-ast-print.cpp b/source/slang/slang-ast-print.cpp index 3608e44f1..e81191086 100644 --- a/source/slang/slang-ast-print.cpp +++ b/source/slang/slang-ast-print.cpp @@ -257,7 +257,7 @@ void ASTPrinter::addGenericParams(const DeclRef<GenericDecl>& genericDeclRef) sb << ">"; } -void ASTPrinter::addDeclParams(const DeclRef<Decl>& declRef, List<Array<Index, 2>>* outParamRange) +void ASTPrinter::addDeclParams(const DeclRef<Decl>& declRef, List<Range<Index>>* outParamRange) { auto& sb = m_builder; @@ -317,7 +317,7 @@ void ASTPrinter::addDeclParams(const DeclRef<Decl>& declRef, List<Array<Index, 2 auto rangeEnd = sb.getLength(); if (outParamRange) - outParamRange->add(makeArray<Index>(rangeStart, rangeEnd)); + outParamRange->add(makeRange<Index>(rangeStart, rangeEnd)); first = false; } diff --git a/source/slang/slang-ast-print.h b/source/slang/slang-ast-print.h index cd8322e2a..0ed3db06b 100644 --- a/source/slang/slang-ast-print.h +++ b/source/slang/slang-ast-print.h @@ -3,6 +3,7 @@ #ifndef SLANG_AST_PRINT_H #define SLANG_AST_PRINT_H +#include "../core/slang-range.h" #include "slang-ast-all.h" namespace Slang { @@ -120,7 +121,7 @@ public: /// Add just the parameters from a declaration. /// Will output the generic parameters (if it's a generic) in <> before the parameters () - void addDeclParams(const DeclRef<Decl>& declRef, List<Array<Index, 2>>* outParamRange = nullptr); + void addDeclParams(const DeclRef<Decl>& declRef, List<Range<Index>>* outParamRange = nullptr); /// Add a prefix that describes the kind of declaration void addDeclKindPrefix(Decl* decl); diff --git a/source/slang/slang-language-server-semantic-tokens.cpp b/source/slang/slang-language-server-semantic-tokens.cpp index 42042e1c0..34c40d8fd 100644 --- a/source/slang/slang-language-server-semantic-tokens.cpp +++ b/source/slang/slang-language-server-semantic-tokens.cpp @@ -414,7 +414,7 @@ void iterateAST(UnownedStringSlice fileName, SourceManager* manager, SyntaxNode* const char* kSemanticTokenTypes[] = { "type", "enumMember", "variable", "parameter", "function", "property", "namespace"}; -static_assert(SLANG_COUNT_OF(kSemanticTokenTypes) == (int)SemanticTokenType::_NormalText, "kSemanticTokenTypes must match SemanticTokenType"); +static_assert(SLANG_COUNT_OF(kSemanticTokenTypes) == (int)SemanticTokenType::NormalText, "kSemanticTokenTypes must match SemanticTokenType"); SemanticToken _createSemanticToken(SourceManager* manager, SourceLoc loc, Name* name) { @@ -424,7 +424,7 @@ SemanticToken _createSemanticToken(SourceManager* manager, SourceLoc loc, Name* token.col = (int)(humaneLoc.column - 1); token.length = name ? (int)(name->text.getLength()) : 0; - token.type = SemanticTokenType::_NormalText; + token.type = SemanticTokenType::NormalText; return token; } @@ -436,7 +436,7 @@ List<SemanticToken> getSemanticTokens(Linkage* linkage, Module* module, UnownedS auto maybeInsertToken = [&](const SemanticToken& token) { if (token.line >= 0 && token.col >= 0 && token.length > 0 && - token.type != SemanticTokenType::_NormalText) + token.type != SemanticTokenType::NormalText) result.add(token); }; diff --git a/source/slang/slang-language-server-semantic-tokens.h b/source/slang/slang-language-server-semantic-tokens.h index e0b8064b5..925972200 100644 --- a/source/slang/slang-language-server-semantic-tokens.h +++ b/source/slang/slang-language-server-semantic-tokens.h @@ -10,9 +10,9 @@ namespace Slang { enum class SemanticTokenType { - Type, EnumMember, Variable, Parameter, Function, Property, Namespace, _NormalText + Type, EnumMember, Variable, Parameter, Function, Property, Namespace, NormalText }; -extern const char* kSemanticTokenTypes[(int)SemanticTokenType::_NormalText]; +extern const char* kSemanticTokenTypes[(int)SemanticTokenType::NormalText]; struct SemanticToken { diff --git a/source/slang/slang-language-server.cpp b/source/slang/slang-language-server.cpp index b8708d48e..dc3b4fe89 100644 --- a/source/slang/slang-language-server.cpp +++ b/source/slang/slang-language-server.cpp @@ -10,6 +10,7 @@ #include <time.h> #include <thread> #include "../core/slang-secure-crt.h" +#include "../core/slang-range.h" #include "../../slang-com-helper.h" #include "../compiler-core/slang-json-rpc-connection.h" #include "slang-language-server-protocol.h" @@ -712,7 +713,7 @@ SlangResult LanguageServer::signatureHelp( SignatureInformation sigInfo; - List<Array<Index, 2>> paramRanges; + List<Slang::Range<Index>> paramRanges; ASTPrinter printer( version->linkage->getASTBuilder(), ASTPrinter::OptionFlag::ParamNames | ASTPrinter::OptionFlag::NoInternalKeywords | @@ -736,8 +737,8 @@ SlangResult LanguageServer::signatureHelp( for (auto& range : paramRanges) { ParameterInformation paramInfo; - paramInfo.label[0] = (uint32_t)range[0]; - paramInfo.label[1] = (uint32_t)range[1]; + paramInfo.label[0] = (uint32_t)range.begin; + paramInfo.label[1] = (uint32_t)range.end; sigInfo.parameters.add(paramInfo); } response.signatures.add(sigInfo); diff --git a/source/slang/slang-workspace-version.cpp b/source/slang/slang-workspace-version.cpp index 7fef7430e..29404c2d0 100644 --- a/source/slang/slang-workspace-version.cpp +++ b/source/slang/slang-workspace-version.cpp @@ -1,6 +1,7 @@ #include "slang-workspace-version.h" #include "../core/slang-io.h" #include "../core/slang-file-system.h" +#include "../core/slang-char-util.h" #include "../compiler-core/slang-lexer.h" namespace Slang @@ -73,34 +74,10 @@ void Workspace::init(List<URI> rootDirURI, slang::IGlobalSession* globalSession) void Workspace::invalidate() { currentVersion = nullptr; } -int parseInt(UnownedStringSlice text, Index& pos) -{ - int result = 0; - while (text[pos] == ' ' && pos < text.getLength()) - { - pos++; - continue; - } - while (pos < text.getLength()) - { - if (text[pos] >= '0' && text[pos] <= '9') - { - result *= 10; - result += text[pos] - '0'; - pos++; - } - else - { - break; - } - } - return result; -} - void parseDiagnostics(Dictionary<String, DocumentDiagnostics>& diagnostics, String compilerOutput) { List<UnownedStringSlice> lines; - StringUtil::split(compilerOutput.getUnownedSlice(), '\n', lines); + StringUtil::calcLines(compilerOutput.getUnownedSlice(), lines); for (Index lineIndex = 0; lineIndex < lines.getCount(); lineIndex++) { auto line = lines[lineIndex]; @@ -116,12 +93,12 @@ void parseDiagnostics(Dictionary<String, DocumentDiagnostics>& diagnostics, Stri LanguageServerProtocol::Diagnostic diagnostic; Index pos = lparentIndex + 1; - int lineLoc = parseInt(line, pos); + int lineLoc = StringUtil::parseInt(line, pos); if (lineLoc == 0) lineLoc = 1; diagnostic.range.end.line = diagnostic.range.start.line = lineLoc - 1; pos++; - int colLoc = parseInt(line, pos); + int colLoc = StringUtil::parseInt(line, pos); if (colLoc == 0) colLoc = 1; diagnostic.range.end.character = diagnostic.range.start.character = colLoc - 1; @@ -148,13 +125,13 @@ void parseDiagnostics(Dictionary<String, DocumentDiagnostics>& diagnostics, Stri continue; } pos = line.indexOf(' '); - diagnostic.code = parseInt(line, pos); + diagnostic.code = StringUtil::parseInt(line, pos); diagnostic.message = line.subString(colonIndex + 2, line.getLength()); if (lineIndex + 1 < lines.getCount() && lines[lineIndex].startsWith("^+")) { lineIndex++; pos = 2; - auto tokenLength = parseInt(lines[lineIndex], pos); + auto tokenLength = StringUtil::parseInt(lines[lineIndex], pos); diagnostic.range.end.character += tokenLength; } diagnosticList.messages.Add(diagnostic); @@ -215,17 +192,6 @@ void* Workspace::getInterface(const Guid& uuid) return nullptr; } -Int convertHexDigit(char c) -{ - if (c >= '0' && c <= '9') - return c - '0'; - if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - return 0; -} - String URI::getPath() const { Index startIndex = uri.indexOf("://"); @@ -244,7 +210,8 @@ String URI::getPath() const auto ch = uri[i]; if (ch == '%') { - Int charVal = convertHexDigit(uri[i + 1]) * 16 + convertHexDigit(uri[i + 2]); + Int charVal = CharUtil::getHexDigitValue(uri[i + 1]) * 16 + + CharUtil::getHexDigitValue(uri[i + 2]); sb.appendChar((char)charVal); i += 3; } @@ -257,6 +224,14 @@ String URI::getPath() const return sb.ProduceString(); } +StringSlice URI::getProtocol() const +{ + Index separatorIndex = uri.indexOf("://"); + if (separatorIndex != -1) + return uri.subString(0, separatorIndex); + return StringSlice(); +} + bool URI::isSafeURIChar(char ch) { return (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || diff --git a/source/slang/slang-workspace-version.h b/source/slang/slang-workspace-version.h index 3dcd3d9ce..58811ed7d 100644 --- a/source/slang/slang-workspace-version.h +++ b/source/slang/slang-workspace-version.h @@ -24,7 +24,7 @@ namespace Slang bool isLocalFile() { return uri.startsWith("file://"); }; String getPath() const; - StringSlice getProtocol() const { return uri.subString(0, uri.indexOf("://")); } + StringSlice getProtocol() const; static URI fromLocalFilePath(UnownedStringSlice path); static URI fromString(UnownedStringSlice uriString); |
