From 7e7d9ce142c3ef077743496cbf8cdc8f669a66af Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 13 Dec 2023 16:39:46 -0800 Subject: Polish language server and documentation. (#3410) Co-authored-by: Yong He --- source/slang/slang-ast-expr.h | 2 + source/slang/slang-ast-iterator.h | 1 + source/slang/slang-check-expr.cpp | 1 + source/slang/slang-language-server-ast-lookup.cpp | 9 +- .../slang-language-server-semantic-tokens.cpp | 147 +++++++++++---------- source/slang/slang-parser.cpp | 1 + 6 files changed, 90 insertions(+), 71 deletions(-) (limited to 'source/slang') diff --git a/source/slang/slang-ast-expr.h b/source/slang/slang-ast-expr.h index 88293a812..23c721038 100644 --- a/source/slang/slang-ast-expr.h +++ b/source/slang/slang-ast-expr.h @@ -53,6 +53,8 @@ class OverloadedExpr : public Expr // arose from a member-reference expression. Expr* base = nullptr; + Expr* originalExpr = nullptr; + // The lookup result that was ambiguous LookupResult lookupResult2; }; diff --git a/source/slang/slang-ast-iterator.h b/source/slang/slang-ast-iterator.h index 7dc358a8e..2e8f02697 100644 --- a/source/slang/slang-ast-iterator.h +++ b/source/slang/slang-ast-iterator.h @@ -150,6 +150,7 @@ struct ASTIterator { iterator->maybeDispatchCallback(expr); dispatchIfNotNull(expr->base); + dispatchIfNotNull(expr->originalExpr); } void visitOverloadedExpr2(OverloadedExpr2* expr) { diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index 753f14768..59f7f2aa1 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -814,6 +814,7 @@ namespace Slang m_astBuilder->getOverloadedType()); overloadedExpr->base = baseExpr; overloadedExpr->lookupResult2 = lookupResult; + overloadedExpr->originalExpr = originalExpr; return overloadedExpr; } else diff --git a/source/slang/slang-language-server-ast-lookup.cpp b/source/slang/slang-language-server-ast-lookup.cpp index 0e8520f3a..3da4f8554 100644 --- a/source/slang/slang-language-server-ast-lookup.cpp +++ b/source/slang/slang-language-server-ast-lookup.cpp @@ -187,7 +187,7 @@ public: declLength = _getDeclNameLength(expr->name, expr->declRef.getDecl()); } if (_isLocInRange( - context, expr->loc, declLength)) + context, expr->loc, declLength)) { ASTLookupResult result; result.path = context->nodePath; @@ -196,7 +196,7 @@ public: return true; } } - + return dispatchIfNotNull(expr->originalExpr); } @@ -242,6 +242,11 @@ public: if (dispatchIfNotNull(expr->base)) return true; } + { + PushNode pushNode(context, expr); + if (dispatchIfNotNull(expr->originalExpr)) + return true; + } if (expr->lookupResult2.getName() && _isLocInRange( context, diff --git a/source/slang/slang-language-server-semantic-tokens.cpp b/source/slang/slang-language-server-semantic-tokens.cpp index 8fb4b1303..837aad06c 100644 --- a/source/slang/slang-language-server-semantic-tokens.cpp +++ b/source/slang/slang-language-server-semantic-tokens.cpp @@ -48,6 +48,78 @@ List getSemanticTokens(Linkage* linkage, Module* module, UnownedS token.type != SemanticTokenType::NormalText) result.add(token); }; + auto handleDeclRef = [&](DeclRef declRef, Expr* originalExpr, SourceLoc loc) + { + if (!declRef) + return; + auto decl = declRef.getDecl(); + if (auto genDecl = as(decl)) + decl = genDecl->inner; + if (!decl) + return; + auto name = declRef.getDecl()->getName(); + if (!name) + return; + // Don't look at the expr if it is defined in a different file. + if (!manager->getHumaneLoc(loc, SourceLocType::Actual) + .pathInfo.foundPath.getUnownedSlice() + .endsWithCaseInsensitive(fileName)) + return; + SemanticToken token = + _createSemanticToken(manager, loc, name); + auto target = decl; + if (as(target)) + { + if (target->hasModifier()) + return; + token.type = SemanticTokenType::Type; + } + else if (as(target)) + { + token.type = SemanticTokenType::Type; + token.length = doc->getTokenLength(token.line, token.col); + } + else if (as(target)) + { + token.type = SemanticTokenType::Type; + } + else if (as(target)) + { + token.type = SemanticTokenType::Property; + } + else if (as(target)) + { + token.type = SemanticTokenType::Parameter; + } + else if (as(target)) + { + if (as(originalExpr) || + as(originalExpr)) + { + return; + } + token.type = SemanticTokenType::Variable; + } + else if (as(target)) + { + token.type = SemanticTokenType::Function; + } + else if (as(target)) + { + token.type = SemanticTokenType::EnumMember; + } + else if (as(target)) + { + token.type = SemanticTokenType::Namespace; + } + + if (as(target)) + { + if (target->hasModifier()) + return; + } + maybeInsertToken(token); + }; iterateAST( fileName, manager, @@ -56,77 +128,14 @@ List getSemanticTokens(Linkage* linkage, Module* module, UnownedS { if (auto declRefExpr = as(node)) { - auto declRef = declRefExpr->declRef; - auto loc = declRefExpr->loc; - if (!declRef) - return; - auto decl = declRef.getDecl(); - if (auto genDecl = as(decl)) - decl = genDecl->inner; - if (!decl) - return; - auto name = declRef.getDecl()->getName(); - if (!name) - return; - // Don't look at the expr if it is defined in a different file. - if (!manager->getHumaneLoc(loc, SourceLocType::Actual) - .pathInfo.foundPath.getUnownedSlice() - .endsWithCaseInsensitive(fileName)) - return; - SemanticToken token = - _createSemanticToken(manager, loc, name); - auto target = decl; - if (as(target)) - { - if (target->hasModifier()) - return; - token.type = SemanticTokenType::Type; - } - else if (as(target)) - { - token.type = SemanticTokenType::Type; - token.length = doc->getTokenLength(token.line, token.col); - } - else if (as(target)) - { - token.type = SemanticTokenType::Type; - } - else if (as(target)) - { - token.type = SemanticTokenType::Property; - } - else if (as(target)) - { - token.type = SemanticTokenType::Parameter; - } - else if (as(target)) - { - if (as(declRefExpr->originalExpr) || - as(declRefExpr->originalExpr)) - { - return; - } - token.type = SemanticTokenType::Variable; - } - else if (as(target)) - { - token.type = SemanticTokenType::Function; - } - else if (as(target)) - { - token.type = SemanticTokenType::EnumMember; - } - else if (as(target)) - { - token.type = SemanticTokenType::Namespace; - } - - if (as(target)) + handleDeclRef(declRefExpr->declRef, declRefExpr->originalExpr, declRefExpr->loc); + } + else if (auto overloadedExpr = as(node)) + { + if (overloadedExpr->lookupResult2.items.getCount()) { - if (target->hasModifier()) - return; + handleDeclRef(overloadedExpr->lookupResult2.items[0].declRef, overloadedExpr->originalExpr, overloadedExpr->loc); } - maybeInsertToken(token); } else if (auto accessorDecl = as(node)) { diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp index 361786b6f..1a7845a25 100644 --- a/source/slang/slang-parser.cpp +++ b/source/slang/slang-parser.cpp @@ -3374,6 +3374,7 @@ namespace Slang { namespaceDecl = parser->astBuilder->create(); namespaceDecl->nameAndLoc = nameAndLoc; + namespaceDecl->loc = nameAndLoc.loc; } } if (!result) -- cgit v1.2.3