Skip to content

Commit

Permalink
Merge pull request #32 from MahdiBM/mmbm-add-on-type-formatting
Browse files Browse the repository at this point in the history
Add on-type formatting support after hitting "return" on non-empty lines
  • Loading branch information
vknabel committed Dec 22, 2023
2 parents 33a5346 + 4a76b5e commit 4e1e5d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/SwiftFormatEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ function format(request: {
export class SwiftFormatEditProvider
implements
vscode.DocumentRangeFormattingEditProvider,
vscode.DocumentFormattingEditProvider
vscode.DocumentFormattingEditProvider,
vscode.OnTypeFormattingEditProvider
{
provideDocumentRangeFormattingEdits(
document: vscode.TextDocument,
Expand All @@ -125,4 +126,16 @@ export class SwiftFormatEditProvider
) {
return format({ document, formatting });
}
provideOnTypeFormattingEdits(
document: vscode.TextDocument,
position: vscode.Position,
ch: string,
formatting: vscode.FormattingOptions,
) {
// Don't format if user has inserted an empty line
if (document.lineAt(position.line).text.trim() === "") {
return [];
}
return format({ document, formatting });
}
}
5 changes: 5 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export function activate(context: vscode.ExtensionContext) {
swiftSelector,
editProvider,
);
vscode.languages.registerOnTypeFormattingEditProvider(
swiftSelector,
editProvider,
"\n",
);
});
}

Expand Down

0 comments on commit 4e1e5d8

Please sign in to comment.