Skip to content

Commit

Permalink
Add tokens for builtin constants (microsoft#1787)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey authored Aug 30, 2021
1 parent a599380 commit adaf6c4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/pylance-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@
"id": "magicFunction",
"description": "magic aka dunder function",
"superType": "function"
},
{
"id": "builtinConstant",
"description": "constants like True, False, None, or __debug__",
"superType": "constant"
}
],
"semanticTokenModifiers": [
Expand Down Expand Up @@ -269,6 +274,9 @@
],
"class.decorator": [
"meta.function.decorator.python"
],
"builtinConstant": [
"constant.language.python"
]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { convertOffsetsToRange, convertOffsetToPosition } from 'pyright-internal
import { doRangesOverlap, Range } from 'pyright-internal/common/textRange';
import {
CaseNode,
ConstantNode,
DecoratorNode,
MatchNode,
ModuleNameNode,
Expand Down Expand Up @@ -62,7 +63,8 @@ enum TokenTypes {
selfParameter = 18,
clsParameter = 19,
magicFunction = 20,
_ = 21,
builtinConstant = 21,
_ = 22,
}

enum TokenModifiers {
Expand Down Expand Up @@ -235,6 +237,12 @@ class TokenWalker extends ParseTreeWalker {
return true;
}

// Emit tokens for builtin constants so that they are colored properly in string-based annotations.
override visitConstant(node: ConstantNode) {
this._pushToken(node, TokenTypes.builtinConstant, TokenModifiers.builtin | TokenModifiers.readonly);
return true;
}

private _isNodeInRange(node: ParseNode) {
if (this._range === undefined) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
//// [|print|]('hello')
//// [|NotImplemented|]
//// raise [|Exception|]()
//// [|None|]
//// [|True|]
//// [|False|]
//// [|__debug__|]
//// x: '[|None|]'

// Covers a few cases of builtins that aren't part of other semantic tests
helper.verifySemanticTokens([
Expand All @@ -14,6 +19,11 @@ helper.verifySemanticTokens([
{ type: 'function', modifiers: ['builtin'] },
{ type: 'variable', modifiers: ['builtin'] },
{ type: 'class', modifiers: ['builtin'] },
{ type: 'builtinConstant', modifiers: ['builtin', 'readonly'] },
{ type: 'builtinConstant', modifiers: ['builtin', 'readonly'] },
{ type: 'builtinConstant', modifiers: ['builtin', 'readonly'] },
{ type: 'builtinConstant', modifiers: ['builtin', 'readonly'] },
{ type: 'builtinConstant', modifiers: ['builtin', 'readonly'] },
],
},
]);
8 changes: 8 additions & 0 deletions packages/vscode-pylance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,11 @@
"id": "magicFunction",
"description": "magic aka dunder function",
"superType": "function"
},
{
"id": "builtinConstant",
"description": "constants like True, False, None, or __debug__",
"superType": "constant"
}
],
"semanticTokenModifiers": [
Expand Down Expand Up @@ -798,6 +803,9 @@
],
"class.decorator": [
"meta.function.decorator.python"
],
"builtinConstant": [
"constant.language.python"
]
}
}
Expand Down

0 comments on commit adaf6c4

Please sign in to comment.