Skip to content

Commit

Permalink
(#69) add format on save configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
takumisoft68 committed Jul 10, 2024
1 parent 644d547 commit c8a6538
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## All notable changes to the "markdowntable" extension will be documented in this file

### 0.12.0

- [Add] Configuration to format on save

### 0.11.0

- [Change] VSCode engine to 1.80.0
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Key binding to `Shift`+`Tab`.
| markdowntable.showMenu.alignRight | Show command in context menu, "Align to Right" | boolean | true |
| markdowntable.showMenu.moveLeft | Show command in context menu, "Move to Left" | boolean | true |
| markdowntable.showMenu.moveRight | Show command in context menu, "Move to Right" | boolean | true |
| markdowntable.formatOnSave | Format all tables on save | boolean | false |

## 4. Tips

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@
"type": "boolean",
"default": true,
"description": "Show command in context menu: \"Move to Right\"."
},
"markdowntable.formatOnSave": {
"type": "boolean",
"default": false,
"description": "Format all tables on save."
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ExtensionContext, Selection, TextDocument, window, StatusBarItem, StatusBarAlignment, TextEditor, TextEditorSelectionChangeEvent } from 'vscode';
import { ContextService } from "./contextService";
import { SupportLanguage } from '../definitions/supportLanguage';

export class TextEditorContextServiceIsSupportedLanguage extends ContextService {
private statusBarItem: StatusBarItem | undefined;
Expand Down Expand Up @@ -39,7 +40,7 @@ export class TextEditorContextServiceIsSupportedLanguage extends ContextService

private updateContextState(editor: TextEditor | undefined) {
const document = editor?.document;
var isSupported = this.isSupportedLanguage(document);
var isSupported = SupportLanguage.isSupportedLanguage(document);

if (isSupported) {
this.setState(true);
Expand All @@ -64,11 +65,4 @@ export class TextEditorContextServiceIsSupportedLanguage extends ContextService
}
}
}

private isSupportedLanguage(document: TextDocument | undefined): boolean {
if(!document || document.languageId === null) {
return false;
}
return (document.languageId === 'markdown' || document.languageId === 'mdx' || document.languageId === 'quarto');
}
}
10 changes: 10 additions & 0 deletions src/definitions/supportLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { TextDocument } from 'vscode';

export class SupportLanguage {
public static isSupportedLanguage(document: TextDocument | undefined): boolean {
if (!document || document.languageId === null) {
return false;
}
return (document.languageId === 'markdown' || document.languageId === 'mdx' || document.languageId === 'quarto');
}
}
29 changes: 29 additions & 0 deletions src/doOnSaveServices/formatOnSaveService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ExtensionContext, window, Disposable, TextDocumentWillSaveEvent, commands, workspace } from 'vscode';
import { SupportLanguage } from '../definitions/supportLanguage';

export class FormatOnSaveService implements Disposable {

public constructor() { }

public activate(context: ExtensionContext) {
context.subscriptions.push(
workspace.onWillSaveTextDocument((event) => this.onWillSaveTextDocument(event))
);
}

public dispose(): void { }

private onWillSaveTextDocument(event: TextDocumentWillSaveEvent): void {
let isSupportedLanguage = SupportLanguage.isSupportedLanguage(event.document);
if (!isSupportedLanguage) {
return;
}

let formatOnSave = <boolean>workspace.getConfiguration("markdowntable").get("formatOnSave");
if (!formatOnSave) {
return;
}

commands.executeCommand("markdowntable.format");
}
}
8 changes: 8 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as vscode from 'vscode';
import * as commands from './commands';
import { ContextServiceManager } from "./contextServices/contextServiceManager";
import { FormatOnSaveService } from './doOnSaveServices/formatOnSaveService';


// this method is called when your extension is activated
Expand All @@ -25,6 +26,13 @@ export function activate(context: vscode.ExtensionContext) {
);
// start custom context key services
contextServiceManager.activate(context);

// subscribe and activate formatOnSaveService
const formatOnSaveService = new FormatOnSaveService();
context.subscriptions.push(
formatOnSaveService
);
formatOnSaveService.activate(context);

// subscribe command handlers
context.subscriptions.push(
Expand Down
7 changes: 7 additions & 0 deletions src/test/sample/demo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# title

| column A | column B | column C |
| :------- | :------- | :------- |
| data | data | data |
| data | | |
| | | |

0 comments on commit c8a6538

Please sign in to comment.