-
Notifications
You must be signed in to change notification settings - Fork 67
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
Add more services to statemachine
example
#1265
Open
Yokozuna59
wants to merge
1
commit into
eclipse-langium:main
Choose a base branch
from
Yokozuna59:yokozuna/add-more-services-to-statemachine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
statemachine TrafficLight | ||
|
||
events | ||
events | ||
switchCapacity | ||
next | ||
|
||
|
151 changes: 151 additions & 0 deletions
151
examples/statemachine/src/language-server/statemachine-code-actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/****************************************************************************** | ||
* Copyright 2021 TypeFox GmbH | ||
* This program and the accompanying materials are made available under the | ||
* terms of the MIT License, which is available in the project root. | ||
******************************************************************************/ | ||
|
||
import type { | ||
AstNode, | ||
DiagnosticData, | ||
LangiumDocument, | ||
MaybePromise, | ||
Reference, | ||
} from 'langium'; | ||
import type { CodeActionProvider } from 'langium/lsp'; | ||
import type { | ||
CodeActionParams, | ||
Command, | ||
CodeAction, | ||
Diagnostic, | ||
TextEdit, | ||
Range, | ||
Position, | ||
} from 'vscode-languageserver'; | ||
import { CodeActionKind } from 'vscode-languageserver'; | ||
import { IssueCodes } from './statemachine-validator.js'; | ||
import type { State, Statemachine } from './generated/ast.js'; | ||
|
||
export class StatemachineCodeActionProvider implements CodeActionProvider { | ||
getCodeActions( | ||
document: LangiumDocument<AstNode>, | ||
params: CodeActionParams | ||
): MaybePromise<Array<Command | CodeAction> | undefined> { | ||
const result: CodeAction[] = []; | ||
const acceptor = (ca: CodeAction | undefined) => ca && result.push(ca); | ||
for (const diagnostic of params.context.diagnostics) { | ||
this.createCodeActions(diagnostic, document, acceptor); | ||
} | ||
return result; | ||
} | ||
|
||
private createCodeActions( | ||
diagnostic: Diagnostic, | ||
document: LangiumDocument, | ||
accept: (ca: CodeAction | undefined) => void | ||
): void { | ||
switch ((diagnostic.data as DiagnosticData)?.code) { | ||
case IssueCodes.StateNameUppercase: | ||
accept(this.makeUpperCase(diagnostic, document)); | ||
break; | ||
case IssueCodes.UnreachedState: | ||
case IssueCodes.UnreachedCommand: | ||
case IssueCodes.UnreachedEvent: | ||
accept(this.removeUnusedSymbol(diagnostic, document)); | ||
break; | ||
} | ||
return undefined; | ||
} | ||
|
||
private makeUpperCase( | ||
diagnostic: Diagnostic, | ||
document: LangiumDocument | ||
): CodeAction { | ||
const changes: TextEdit[] = []; | ||
|
||
const stateName = document.textDocument.getText(diagnostic.range); | ||
const { init, states } = document.parseResult.value as Statemachine; | ||
this.updateChangesForReferencedState(init, stateName, document, changes); | ||
|
||
states.forEach(({ transitions }) => { | ||
transitions.forEach(({ state }) => { | ||
this.updateChangesForReferencedState( | ||
state, | ||
stateName, | ||
document, | ||
changes | ||
); | ||
}); | ||
}); | ||
|
||
const range = this.getFirstLetterRange(diagnostic.range.start); | ||
changes.push(this.createTextEditForState(range, document)); | ||
return { | ||
title: 'First letter to upper case', | ||
kind: CodeActionKind.QuickFix, | ||
diagnostics: [diagnostic], | ||
isPreferred: true, | ||
edit: { | ||
changes: { | ||
[document.textDocument.uri]: changes, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
private createTextEditForState( | ||
range: Range, | ||
document: LangiumDocument | ||
): TextEdit { | ||
const changeRange = this.getFirstLetterRange(range.start); | ||
return { | ||
range: changeRange, | ||
newText: document.textDocument.getText(changeRange).toUpperCase(), | ||
}; | ||
} | ||
|
||
private updateChangesForReferencedState( | ||
state: Reference<State>, | ||
name: string, | ||
document: LangiumDocument, | ||
changes: TextEdit[] | ||
): void { | ||
if (state.$refNode && state.ref && state.ref.name === name) { | ||
const { range } = state.$refNode; | ||
const changeRange = this.getFirstLetterRange(range.start); | ||
changes.push(this.createTextEditForState(changeRange, document)); | ||
} | ||
} | ||
|
||
private getFirstLetterRange(position: Position): Range { | ||
const range: Range = { | ||
start: position, | ||
end: { | ||
line: position.line, | ||
character: position.character + 1, | ||
}, | ||
}; | ||
return range; | ||
} | ||
|
||
private removeUnusedSymbol( | ||
diagnostic: Diagnostic, | ||
document: LangiumDocument | ||
): CodeAction { | ||
return { | ||
title: 'Remove unsed symbol', | ||
kind: CodeActionKind.QuickFix, | ||
diagnostics: [diagnostic], | ||
isPreferred: true, | ||
edit: { | ||
changes: { | ||
[document.textDocument.uri]: [ | ||
{ | ||
range: diagnostic.range, | ||
newText: '', | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
examples/statemachine/src/language-server/statemachine-formatter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/****************************************************************************** | ||
* Copyright 2021 TypeFox GmbH | ||
* This program and the accompanying materials are made available under the | ||
* terms of the MIT License, which is available in the project root. | ||
******************************************************************************/ | ||
|
||
import type { AstNode } from 'langium'; | ||
import { AbstractFormatter, Formatting } from 'langium/lsp'; | ||
import * as ast from './generated/ast.js'; | ||
|
||
export class StatemachineFormatter extends AbstractFormatter { | ||
protected format(node: AstNode): void { | ||
if (ast.isState(node)) { | ||
const formatter = this.getNodeFormatter(node); | ||
formatter.keyword('state') | ||
.prepend(Formatting.newLine({ allowMore: true })) | ||
.append(Formatting.oneSpace()); | ||
|
||
formatter.keyword('actions').append(Formatting.oneSpace()); | ||
const bracesOpen = formatter.keyword('{'); | ||
bracesOpen.prepend(Formatting.fit(Formatting.oneSpace(), Formatting.newLine())); | ||
const bracesClose = formatter.keyword('}'); | ||
bracesClose.prepend(Formatting.newLine()); | ||
formatter.interior(bracesOpen, bracesClose).prepend(Formatting.indent()); | ||
|
||
const stateName = formatter.property('name'); | ||
const stateEnd = formatter.keyword('end'); | ||
formatter.interior(stateName, stateEnd).prepend(Formatting.indent()); | ||
stateEnd.prepend(Formatting.newLine()); | ||
} else if (ast.isStatemachine(node)) { | ||
const formatter = this.getNodeFormatter(node); | ||
|
||
formatter.keyword('statemachine').append(Formatting.oneSpace()); | ||
formatter.properties('name').append(Formatting.newLine({ allowMore: true })); | ||
|
||
formatter.keyword('initialState') | ||
.prepend(Formatting.newLine({ allowMore: true })) | ||
.append(Formatting.oneSpace()); | ||
formatter.property('init').append(Formatting.newLine({ allowMore: true })); | ||
|
||
formatter.keyword('commands') | ||
.prepend(Formatting.newLine({ allowMore: true })); | ||
formatter.keyword('events') | ||
.prepend(Formatting.newLine({ allowMore: true })); | ||
const nodes = formatter.nodes(...node.commands, ...node.events); | ||
nodes.prepend(Formatting.indent()); | ||
} else if (ast.isTransition(node)) { | ||
const formatter = this.getNodeFormatter(node); | ||
formatter.keyword('=>').surround(Formatting.oneSpace()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing, how to double indent? Currently, the following code:
Would be formatted to:
I tried to do the following:
But it didn't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took a look at this, and this seems to be an issue in the formatter. This is probably something we want to fix in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I think the outer indent (the one for
state
andend
) overrides this one.Or maybe it doesn't take the double indent in consideration.