forked from CrossBreezeNL/crossmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validation: The filename and it's id in the file should be in sync (C…
…rossBreezeNL#76) - Add warning if filename and element id do not match - Provide quick fix to properly rename file Minor: - Fix watch scripts for browser and electron to use package name - Fix wrong translation of diagnostic severity
- Loading branch information
1 parent
023d330
commit ff0669f
Showing
14 changed files
with
180 additions
and
23 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
44 changes: 44 additions & 0 deletions
44
extensions/crossmodel-lang/src/language-server/cross-model-code-action-provider.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,44 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2024 CrossBreeze. | ||
********************************************************************************/ | ||
|
||
import { ModelFileExtensions } from '@crossbreeze/protocol'; | ||
import { UriUtils, type LangiumDocument } from 'langium'; | ||
import type { CodeActionProvider } from 'langium/lsp'; | ||
import { RenameFile, type CodeAction, type CodeActionParams } from 'vscode-languageserver-protocol'; | ||
import { FilenameNotMatchingDiagnostic } from './cross-model-validator.js'; | ||
import { findSemanticRoot } from './util/ast-util.js'; | ||
|
||
export class CrossModelCodeActionProvider implements CodeActionProvider { | ||
getCodeActions(document: LangiumDocument, params: CodeActionParams): CodeAction[] { | ||
const result: CodeAction[] = []; | ||
const accept = (action: CodeAction | undefined): void => { | ||
if (action) { | ||
result.push(action); | ||
} | ||
}; | ||
for (const diagnostic of params.context.diagnostics) { | ||
if (FilenameNotMatchingDiagnostic.is(diagnostic)) { | ||
accept(this.fixFilenameNotMatching(diagnostic, document)); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private fixFilenameNotMatching(diagnostic: FilenameNotMatchingDiagnostic, document: LangiumDocument): CodeAction | undefined { | ||
const semanticRoot = findSemanticRoot(document); | ||
if (!semanticRoot || !semanticRoot.id) { | ||
return undefined; | ||
} | ||
const newName = semanticRoot.id + ModelFileExtensions.getFileExtension(document.uri.toString()); | ||
const newUri = UriUtils.joinPath(UriUtils.dirname(document.uri), newName); | ||
return { | ||
title: `Rename file to '${newName}'`, | ||
edit: { | ||
documentChanges: [RenameFile.create(document.uri.toString(), newUri.toString())] | ||
}, | ||
kind: 'quickfix', | ||
diagnostics: [diagnostic] | ||
}; | ||
} | ||
} |
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
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
23 changes: 23 additions & 0 deletions
23
extensions/crossmodel-lang/test/language-server/cross-model-filename.test.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,23 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2024 CrossBreeze. | ||
********************************************************************************/ | ||
|
||
import { ModelFileExtensions } from '@crossbreeze/protocol'; | ||
import { entity1 } from './test-utils/test-documents/entity/entity1.js'; | ||
import { createCrossModelTestServices, parseEntity, testUri } from './test-utils/utils.js'; | ||
|
||
const services = createCrossModelTestServices(); | ||
|
||
describe('CrossModel Filename Validation', () => { | ||
test('Mismatching id and filename does not yield error', async () => { | ||
const entity = await parseEntity({ | ||
services, | ||
text: entity1, | ||
validation: true, | ||
documentUri: testUri('Customer2' + ModelFileExtensions.Entity) | ||
}); | ||
expect(entity.id).toBe('Customer'); | ||
expect(entity.$document.diagnostics).toHaveLength(1); | ||
expect(entity.$document.diagnostics![0].message).toContain('Filename should match element id'); | ||
}); | ||
}); |
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
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
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