-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added editing features to Langium-based example
- Loading branch information
1 parent
918b000
commit c2a70b1
Showing
16 changed files
with
195 additions
and
65 deletions.
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
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
89 changes: 89 additions & 0 deletions
89
examples/states-langium/language-server/src/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,89 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2023 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { LangiumDocument, MaybePromise, URI } from 'langium'; | ||
import { CodeActionProvider } from 'langium/lsp'; | ||
import { CodeActionParams, Command, CodeAction, Position, WorkspaceEdit } from 'vscode-languageserver'; | ||
import { StateMachine } from './generated/ast.js'; | ||
|
||
const CREATE_STATE_KIND = 'sprotty.create.state'; | ||
const CREATE_EVENT_KIND = 'sprotty.create.event'; | ||
|
||
export class StatesCodeActionProvider implements CodeActionProvider { | ||
|
||
getCodeActions(document: LangiumDocument<StateMachine>, params: CodeActionParams): MaybePromise<(Command | CodeAction)[] | undefined> { | ||
const sm = document.parseResult.value; | ||
const result: CodeAction[] = []; | ||
const endOfDocument = document.textDocument.positionAt(document.textDocument.getText().length); | ||
if (matchesContext(CREATE_STATE_KIND, params)) { | ||
result.push({ | ||
kind: CREATE_STATE_KIND, | ||
title: 'new State', | ||
edit: createInsertWorkspaceEdit( | ||
document.uri, | ||
endOfDocument, | ||
'\n' + 'state ' + getNewName('state', sm.states.map(s => s.name)) | ||
) | ||
}); | ||
} | ||
if (matchesContext(CREATE_EVENT_KIND, params)) { | ||
result.push({ | ||
kind: CREATE_EVENT_KIND, | ||
title: 'new Event', | ||
edit: createInsertWorkspaceEdit( | ||
document.uri, | ||
endOfDocument, | ||
'\n' + 'event '+ getNewName('event', sm.events.map(e => e.name)) | ||
) | ||
}); | ||
} | ||
return result; | ||
} | ||
|
||
} | ||
|
||
function matchesContext(kind: string, params: CodeActionParams): boolean { | ||
if (!params.context?.only) { | ||
return true; | ||
} else { | ||
return params.context.only.some(k => kind.startsWith(k)); | ||
} | ||
} | ||
|
||
function getNewName(prefix: string, siblings: string[]): string { | ||
for (let i = 0;; i++) { | ||
const currentName = prefix + i; | ||
if (!siblings.some(s => s === currentName)) { | ||
return currentName; | ||
} | ||
} | ||
} | ||
|
||
function createInsertWorkspaceEdit(uri: URI, position: Position, text: string): WorkspaceEdit { | ||
return { | ||
changes: { | ||
[uri.toString()]: [ | ||
{ | ||
range: { | ||
start: position, | ||
end: position | ||
}, | ||
newText: text | ||
} | ||
] | ||
} | ||
}; | ||
} |
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
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
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,15 @@ | ||
.sprotty-popup { | ||
padding: 0; | ||
color: var(--vscode-editorHoverWidget-foreground); | ||
background: var(--vscode-editorHoverWidget-background); | ||
border-color: var(--vscode-editorHoverWidget-border); | ||
} | ||
|
||
.sprotty-palette > div { | ||
padding: 0 4px; | ||
cursor: pointer; | ||
} | ||
|
||
.sprotty-palette > div:hover { | ||
color: var(--vscode-textLink-activeForeground); | ||
} |
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
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.