Skip to content

Commit

Permalink
Merge pull request #99 from tintinweb/auto-formatting
Browse files Browse the repository at this point in the history
Auto formatting
  • Loading branch information
tintinweb committed May 10, 2022
2 parents 28f1f1f + bb3d483 commit a6b963a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
"jquery-mousewheel": "^3.1.13",
"lodash": "^4.17.21",
"text-encoding": "^0.7.0",
"ts-graphviz": "^0.16.0",
"vscode-uri": "^3.0.3"
},
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import InteractiveWebviewGenerator from "./features/interactiveWebview";
import PreviewPanel from "./features/previewPanel";
import ColorProvider from "./language/ColorProvider";
import DotCompletionItemProvider from "./language/CompletionItemProvider";
import DotDocumentFormatter from "./language/DocumentFormatter";
import DotHoverProvider from "./language/HoverProvider";
import SymbolProvider from "./language/SymbolProvider";
import * as settings from "./settings";
Expand Down Expand Up @@ -117,6 +118,10 @@ function onActivate(context: vscode.ExtensionContext) {
[settings.languageId],
symProvider,
));
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(
[settings.languageId],
new DotDocumentFormatter(),
));
}

/* exports */
Expand Down
42 changes: 42 additions & 0 deletions src/language/DocumentFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { parse } from "@ts-graphviz/parser";
import { toDot } from "ts-graphviz";
import {
DocumentFormattingEditProvider,
// FormattingOptions,
Position,
ProviderResult,
Range,
TextDocument,
TextEdit,
window,
} from "vscode";

export default class DotDocumentFormatter implements DocumentFormattingEditProvider {

This comment has been minimized.

Copy link
@FFengIll

FFengIll May 13, 2022

this formatter did too much.
e.g. it did an auto sort and add quotes for all node.

it is good but should be optional.

// eslint-disable-next-line class-methods-use-this
provideDocumentFormattingEdits(
document: TextDocument,
// options: FormattingOptions,
// token: CancellationToken,
): ProviderResult<TextEdit[]> {
try {
const d = parse(document.getText());
const dot = toDot(d);

const edit: TextEdit[] = [];
edit.push(new TextEdit(
new Range(
new Position(0, 0),
new Position(document.lineCount - 1, document.lineAt(document.lineCount - 1).text.length),
),
dot,
));
return edit;
} catch (e: any) {
// (parser error) don't bubble up as a pot. unhandled thenable promise;
// explicitly return "no change" instead.
// show error message
window.showErrorMessage(`${e.name} (@${e.location.start.line}:${e.location.start.column}): ${e.message}`);
return undefined;
}
}
}
5 changes: 4 additions & 1 deletion src/language/SymbolProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ implements
}

// eslint-disable-next-line class-methods-use-this
private containingSymbol(symbols: DocumentSymbolInformation[], position: Position) {
private containingSymbol(
symbols: DocumentSymbolInformation[],
position: Position,
) {
let closestSymbol: DocumentSymbolInformation|undefined;

const crawler = (syms: DocumentSymbolInformation[]) => {
Expand Down

0 comments on commit a6b963a

Please sign in to comment.