| 86 | } |
| 87 | |
| 88 | void SemanticTokensBuilder::encode( |
| 89 | SourceLocation const& _sourceLocation, |
| 90 | SemanticTokenType _tokenType, |
| 91 | SemanticTokenModifiers _modifiers |
| 92 | ) |
| 93 | { |
| 94 | /* |
| 95 | https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#textDocument_semanticTokens |
| 96 | |
| 97 | // Step-1: Absolute positions |
| 98 | { line: 2, startChar: 5, length: 3, tokenType: 0, tokenModifiers: 3 }, |
| 99 | { line: 2, startChar: 10, length: 4, tokenType: 1, tokenModifiers: 0 }, |
| 100 | { line: 5, startChar: 2, length: 7, tokenType: 2, tokenModifiers: 0 } |
| 101 | |
| 102 | // Step-2: Relative positions as intermediate step |
| 103 | { deltaLine: 2, deltaStartChar: 5, length: 3, tokenType: 0, tokenModifiers: 3 }, |
| 104 | { deltaLine: 0, deltaStartChar: 5, length: 4, tokenType: 1, tokenModifiers: 0 }, |
| 105 | { deltaLine: 3, deltaStartChar: 2, length: 7, tokenType: 2, tokenModifiers: 0 } |
| 106 | |
| 107 | // Step-3: final array result |
| 108 | // 1st token, 2nd token, 3rd token |
| 109 | [ 2,5,3,0,3, 0,5,4,1,0, 3,2,7,2,0 ] |
| 110 | |
| 111 | So traverse through the AST and assign each leaf a token 5-tuple. |
| 112 | */ |
| 113 | |
| 114 | // solAssert(_sourceLocation.isValid()); |
| 115 | if (!_sourceLocation.isValid()) |
| 116 | return; |
| 117 | |
| 118 | auto const [line, startChar] = m_charStream->translatePositionToLineColumn(_sourceLocation.start); |
| 119 | auto const length = _sourceLocation.end - _sourceLocation.start; |
| 120 | |
| 121 | lspDebug(fmt::format("encode [{}:{}..{}] {}", line, startChar, length, static_cast<int>(_tokenType))); |
| 122 | |
| 123 | m_encodedTokens.emplace_back(line - m_lastLine); |
| 124 | if (line == m_lastLine) |
| 125 | m_encodedTokens.emplace_back(startChar - m_lastStartChar); |
| 126 | else |
| 127 | m_encodedTokens.emplace_back(startChar); |
| 128 | m_encodedTokens.emplace_back(length); |
| 129 | m_encodedTokens.emplace_back(static_cast<int>(_tokenType)); |
| 130 | m_encodedTokens.emplace_back(static_cast<int>(_modifiers)); |
| 131 | |
| 132 | m_lastLine = line; |
| 133 | m_lastStartChar = startChar; |
| 134 | } |
| 135 | |
| 136 | bool SemanticTokensBuilder::visit(frontend::ContractDefinition const& _node) |
| 137 | { |