Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f50052c

Browse files
committed
更新格式化的功能
更新格式化的功能
1 parent 9849da2 commit f50052c

File tree

5 files changed

+53
-69
lines changed

5 files changed

+53
-69
lines changed

‎.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"compile-hero.disable-compile-files-on-did-save-code": true
2+
"compile-hero.disable-compile-files-on-did-save-code": true,
3+
"vue3snippets.enable-compile-vue-file-on-did-save-code": false
34
}

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Vue3 Snippets, Contains code highlighting, code snippets and formatting commonly
1313

1414
You can turn on the statusbar `Auto Format Vue` switch at the bottom of vscode, which allows you to automatically format the `vue` file when you write it.
1515

16+
Or right-click to display the drop-down menu panel, click the `Vue Format` menu item to format.
17+
1618
<!-- <img src="./public/2.gif" /> -->
1719
![Demo](public/2.gif)
1820

‎extension.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,32 @@ function activate(context) {
1313
config.update("enable-compile-vue-file-on-did-save-code", false);
1414
statusBarUi.StatusBarUi.notWatching();
1515
});
16+
let format = vscode.commands.registerCommand("vue3snippets.format", () => {
17+
const editor = vscode.window.activeTextEditor;
18+
const filepath = editor.document.uri.fsPath;
19+
if (!editor) throw new Error('no active editor');
20+
const doc = editor.document;
21+
const lineCount = doc.lineCount;
22+
const text = doc.getText();
23+
const start = new vscode.Position(0, 0);
24+
const end = new vscode.Position(lineCount + 1, 0);
25+
const range = new vscode.Range(start, end);
26+
const prettierText = prettier.format(text, { filepath });
27+
editor.edit((editBuilder, error) => {
28+
error && window.showErrorMessage(error);
29+
editBuilder.replace(range, prettierText);
30+
});
31+
});
1632
context.subscriptions.push(compileOn);
1733
context.subscriptions.push(compileOff);
18-
vscode.workspace.onWillSaveTextDocument(({ document }) => {
34+
context.subscriptions.push(format);
35+
vscode.workspace.onWillSaveTextDocument(() => {
1936
let config = vscode.workspace.getConfiguration("vue3snippets");
2037
let isEnableOnDidSaveTextDocument = config.get("enable-compile-vue-file-on-did-save-code");
2138
if (!isEnableOnDidSaveTextDocument) { return };
2239
let activeTextEditor = vscode.window.activeTextEditor;
2340
if (activeTextEditor && activeTextEditor.document.languageId === 'vue') {
24-
const filepath = document.uri.fsPath;
25-
const editor = vscode.window.activeTextEditor;
26-
if (!editor) throw new Error('no active editor');
27-
const doc = editor.document;
28-
const lineCount = doc.lineCount;
29-
const text = doc.getText();
30-
const start = new vscode.Position(0, 0);
31-
const end = new vscode.Position(lineCount + 1, 0);
32-
const range = new vscode.Range(start, end);
33-
const prettierText = prettier.format(text, { filepath });
34-
editor.edit((editBuilder, error) => {
35-
error && window.showErrorMessage(error);
36-
editBuilder.replace(range, prettierText);
37-
});
38-
} else {
39-
// vscode.window.showInformationMessage('It‘s not a .vue file');
41+
vscode.commands.executeCommand("vue3snippets.format");
4042
}
4143
});
4244
statusBarUi.StatusBarUi.init(vscode.workspace.getConfiguration("vue3snippets").get("enable-compile-vue-file-on-did-save-code"));

‎package.json

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"url": "https://github.com/Wscats"
1010
},
1111
"publisher": "Wscats",
12-
"version": "1.0.9",
12+
"version": "1.0.10",
1313
"license": "MIT",
1414
"engines": {
1515
"vscode": "^1.40.0"
@@ -21,7 +21,8 @@
2121
"activationEvents": [
2222
"*",
2323
"onCommand:vue3snippets.compileOn",
24-
"onCommand:vue3snippets.compileOff"
24+
"onCommand:vue3snippets.compileOff",
25+
"onCommand:vue3snippets.format"
2526
],
2627
"main": "./extension.js",
2728
"keywords": [
@@ -55,62 +56,20 @@
5556
"Formatters"
5657
],
5758
"contributes": {
59+
"commands": [
60+
{
61+
"when": "editorLangId == vue",
62+
"command": "vue3snippets.format",
63+
"title": "Vue Format"
64+
}
65+
],
5866
"configuration": {
5967
"title": "Auto Format Vue",
6068
"properties": {
6169
"vue3snippets.enable-compile-vue-file-on-did-save-code": {
6270
"type": "boolean",
6371
"default": false,
6472
"description": "Enable compile vue file on did save code."
65-
},
66-
"vue3snippets.html_indent_root": {
67-
"type": "boolean",
68-
"default": false,
69-
"description": "If indent the root-tag in .vue's template."
70-
},
71-
"vue3snippets.format_need": {
72-
"type": "array",
73-
"default": [
74-
"html",
75-
"js",
76-
"css"
77-
],
78-
"description": "List of format, default [\"html\", \"js\", \"css\"]."
79-
},
80-
"vue3snippets.break_attr_limit": {
81-
"type": "number",
82-
"default": -1,
83-
"description": "Break attributes when tag's attributes.length > this number, no break when -1."
84-
},
85-
"vue3snippets.attr_end_with_gt": {
86-
"type": "boolean",
87-
"default": true,
88-
"description": "If end attrs width '>' when break_attr_limit."
89-
},
90-
"vue3snippets.js-beautify": {
91-
"type": "object",
92-
"default": {
93-
"indent_size": "editor.tabSize",
94-
"indent_char": " ",
95-
"indent_with_tabs": false,
96-
"brace-style": "collapse",
97-
"space_after_anon_function": true,
98-
"css": {},
99-
"js": {},
100-
"html": {
101-
"force_format": [
102-
"template"
103-
]
104-
}
105-
},
106-
"description": "The config use some js-beautify options, see js-beautify."
107-
},
108-
"vue3snippets.pug-beautify": {
109-
"type": "object",
110-
"default": {
111-
"fill_tab": false
112-
},
113-
"description": "The config use some pug-beautify options, see pug-beautify."
11473
}
11574
}
11675
},
@@ -133,6 +92,23 @@
13392
"path": "./syntaxes/vue.tmLanguage"
13493
}
13594
],
95+
"menus": {
96+
"editor/context": [
97+
{
98+
"when": "editorLangId == vue",
99+
"command": "vue3snippets.format",
100+
"group": "navigation"
101+
}
102+
]
103+
},
104+
"keybindings": [
105+
{
106+
"key": "ctrl+shift+f",
107+
"mac": "cmd+shift+f",
108+
"when": "editorTextFocus && !editorReadonly && editorLangId == vue",
109+
"command": "vue3snippets.format"
110+
}
111+
],
136112
"snippets": [
137113
{
138114
"language": "javascript",

‎tests/test.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<p>123</p>
3+
</template>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /