Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mutate keyword in hover documentation for parameters #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions server/src/lsp-features/find-locals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface TolkLocalVariable {
node: Parser.SyntaxNode,
name: string
type: TolkType,
mutate: boolean, // if parameter has `mutate` keyword
}

export function findLocalVariables(rootNode: Parser.SyntaxNode, cursorPosition: Parser.Point): TolkLocalVariable[] {
Expand All @@ -26,7 +27,8 @@ export function findLocalVariables(rootNode: Parser.SyntaxNode, cursorPosition:
kind: 'variable',
node: nameNode,
name: extractNameFromNode(nameNode),
type: extractType(potentialVarNode.childForFieldName('type'))
type: extractType(potentialVarNode.childForFieldName('type')),
mutate: false
})
}
}
Expand All @@ -42,7 +44,8 @@ export function findLocalVariables(rootNode: Parser.SyntaxNode, cursorPosition:
kind: 'parameter',
node: nameNode,
name: extractNameFromNode(nameNode),
type: extractType(paramNode.childForFieldName('type'))
type: extractType(paramNode.childForFieldName('type')),
mutate: paramNode.childForFieldName('modifiers') !== null
})
}
}
Expand Down
4 changes: 3 additions & 1 deletion server/src/lsp-features/lsp-hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ILspHandler } from '../connection'
import { findLocalVariables, TolkLocalVariable } from './find-locals'
import { stringifyType } from './type-inference'
import { extractNameFromNode, TolkDocumentSymbol } from './lsp-document-symbols'
import { MarkupKind } from "vscode-languageserver";

export class HoverLspHandler implements ILspHandler {
constructor(
Expand Down Expand Up @@ -76,11 +77,12 @@ export class HoverLspHandler implements ILspHandler {
}

private stringifyLocalVariable(symbol: TolkLocalVariable): string {
const mutability = symbol.mutate ? 'mutable ' : ''
const strVal = symbol.kind === 'parameter' ? 'param ' : 'var '
if (symbol.type.kind === 'auto' || symbol.type.kind === 'function') {
// return strVal + symbol.name
}
return strVal + symbol.name + ': ' + stringifyType(symbol.type)
return mutability + strVal + symbol.name + ': ' + stringifyType(symbol.type)
}

private stringifyGlobalSymbol(symbol: TolkDocumentSymbol): string {
Expand Down
Binary file modified server/tree-sitter-tolk.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion tree-sitter-tolk/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const TOLK_GRAMMAR = {
')'
),
parameter_declaration: $ => seq(
optional('mutate'),
optional(field('modifiers', 'mutate')),
field('name', $.identifier),
optional(seq(
':',
Expand Down
9 changes: 6 additions & 3 deletions tree-sitter-tolk/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,12 @@
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "mutate"
"type": "FIELD",
"name": "modifiers",
"content": {
"type": "STRING",
"value": "mutate"
}
},
{
"type": "BLANK"
Expand Down Expand Up @@ -2473,4 +2477,3 @@
"inline": [],
"supertypes": []
}

10 changes: 10 additions & 0 deletions tree-sitter-tolk/src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,16 @@
"type": "parameter_declaration",
"named": true,
"fields": {
"modifiers": {
"multiple": false,
"required": false,
"types": [
{
"type": "mutate",
"named": false
}
]
},
"name": {
"multiple": false,
"required": true,
Expand Down
Loading