Skip to content

Commit

Permalink
Prettyhtml. Fix #561. Fix #491
Browse files Browse the repository at this point in the history
  • Loading branch information
octref committed Sep 25, 2018
1 parent 4e83011 commit 0b4e385
Show file tree
Hide file tree
Showing 5 changed files with 884 additions and 28 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,12 @@
"default": "none",
"enum": [
"none",
"prettyhtml",
"js-beautify-html"
],
"enumDescriptions": [
"disable formatting",
"prettyhtml",
"html formatter of js-beautify"
],
"description": "Default formatter for <template> region"
Expand Down
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"homepage": "https://github.com/vuejs/vetur/tree/master/server",
"dependencies": {
"@starptech/prettyhtml": "^0.0.60",
"bootstrap-vue-helper-json": "^1.1.1",
"buefy-helper-json": "^1.0.2",
"element-helper-json": "^2.0.2",
Expand Down
3 changes: 0 additions & 3 deletions server/src/modes/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ export function getVueHTMLMode(
return findDocumentSymbols(document, vueDocuments.get(document));
},
format(document: TextDocument, range: Range, formattingOptions: FormattingOptions) {
if (config.vetur.format.defaultFormatter.html === 'none') {
return [];
}
return htmlFormat(document, range, formattingOptions, config);
},
findDefinition(document: TextDocument, position: Position) {
Expand Down
39 changes: 30 additions & 9 deletions server/src/modes/template/services/htmlFormat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as _ from 'lodash';
import { TextDocument, Range, TextEdit, Position, FormattingOptions } from 'vscode-languageserver-types';
import { html as htmlBeautify } from 'js-beautify';
import * as prettyhtml from '@starptech/prettyhtml';

const templateHead = '<template>';
const templateTail = '</template>';
Expand All @@ -11,18 +12,19 @@ export function htmlFormat(
formattingOptions: FormattingOptions,
config: any
): TextEdit[] {
const { value, range } = getValueAndRange(document, currRange);
if (config.vetur.format.defaultFormatter.html === 'none') {
return [];
}

defaultHtmlOptions.indent_with_tabs = !formattingOptions.insertSpaces;
defaultHtmlOptions.indent_size = formattingOptions.tabSize;
const { value, range } = getValueAndRange(document, currRange);

const htmlFormattingOptions = _.assign(
defaultHtmlOptions,
config.vetur.format.defaultFormatterOptions['js-beautify-html'],
{ end_with_newline: false }
);
let beautifiedHtml: string;
if (config.vetur.format.defaultFormatter.html === 'prettyhtml') {
beautifiedHtml = formatWithPrettyHtml(templateHead + value + templateTail);
} else {
beautifiedHtml = formatWithJsBeautify(templateHead + value + templateTail, formattingOptions, config);
}

const beautifiedHtml = htmlBeautify(templateHead + value + templateTail, htmlFormattingOptions);
const wrappedHtml = beautifiedHtml.substring(templateHead.length, beautifiedHtml.length - templateTail.length);
return [
{
Expand All @@ -32,6 +34,25 @@ export function htmlFormat(
];
}

function formatWithPrettyHtml(input: string): string {
const result = prettyhtml(input, {});
return result.trim();
}

function formatWithJsBeautify(input: string, formattingOptions: FormattingOptions, config: any): string {
const htmlFormattingOptions = _.assign(
defaultHtmlOptions,
{
indent_with_tabs: !formattingOptions.insertSpaces,
indent_size: formattingOptions.tabSize
},
config.vetur.format.defaultFormatterOptions['js-beautify-html'],
{ end_with_newline: false }
);

return htmlBeautify(input, htmlFormattingOptions);
}

function getValueAndRange(document: TextDocument, currRange: Range): { value: string; range: Range } {
let value = document.getText();
let range = currRange;
Expand Down
Loading

0 comments on commit 0b4e385

Please sign in to comment.