-
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.
feat: CLI command to run formatting (#824)
Closes #702 ### Summary of Changes Add a new `format` command to format Safe-DS files.
- Loading branch information
1 parent
55b5ed0
commit a74b8e0
Showing
7 changed files
with
116 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { createSafeDsServicesWithBuiltins } from '@safe-ds/lang'; | ||
import { NodeFileSystem } from 'langium/node'; | ||
import { extractDocuments } from '../helpers/documents.js'; | ||
import { exitIfDocumentHasSyntaxErrors } from '../helpers/diagnostics.js'; | ||
import { TextDocument } from 'vscode-languageserver-textdocument'; | ||
import { writeFile } from 'node:fs/promises'; | ||
import chalk from 'chalk'; | ||
|
||
export const format = async (fsPaths: string[]): Promise<void> => { | ||
const services = (await createSafeDsServicesWithBuiltins(NodeFileSystem)).SafeDs; | ||
const documents = await extractDocuments(services, fsPaths); | ||
|
||
// Exit if any document has syntax errors before formatting code | ||
for (const document of documents) { | ||
exitIfDocumentHasSyntaxErrors(document); | ||
} | ||
|
||
// Format code | ||
for (const document of documents) { | ||
const edits = await services.lsp.Formatter!.formatDocument(document, { | ||
textDocument: { | ||
uri: document.uri.toString(), | ||
}, | ||
options: { | ||
tabSize: 4, | ||
insertSpaces: true, | ||
}, | ||
}); | ||
|
||
const editedDocument = TextDocument.applyEdits(document.textDocument, edits); | ||
await writeFile(document.uri.fsPath, editedDocument); | ||
} | ||
|
||
console.log(chalk.green(`Safe-DS code formatted successfully.`)); | ||
}; |
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
3 changes: 3 additions & 0 deletions
3
packages/safe-ds-cli/tests/resources/format/contains syntax errors.sdstest
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,3 @@ | ||
package test | ||
|
||
segment mySegment() |
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,3 @@ | ||
package test | ||
|
||
pipeline myPipeline {} |
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,3 @@ | ||
package test | ||
|
||
segment mySegment() {} |