mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
[html] add formatter
This commit is contained in:
parent
bb9a0f89ad
commit
49dfae7e00
|
@ -36,8 +36,8 @@ connection.onInitialize((params: InitializeParams): InitializeResult => {
|
|||
// Tell the client that the server works in FULL text document sync mode
|
||||
textDocumentSync: documents.syncKind,
|
||||
completionProvider: { resolveProvider: false, triggerCharacters: ['"', ':'] },
|
||||
hoverProvider: true,
|
||||
documentSymbolProvider: true,
|
||||
// hoverProvider: true,
|
||||
// documentSymbolProvider: true,
|
||||
documentRangeFormattingProvider: true,
|
||||
documentFormattingProvider: true
|
||||
}
|
||||
|
@ -138,15 +138,32 @@ connection.onCompletion(textDocumentPosition => {
|
|||
// return languageService.findDocumentSymbols(document, htmlDocument);
|
||||
// });
|
||||
|
||||
// connection.onDocumentFormatting(formatParams => {
|
||||
// let document = documents.get(formatParams.textDocument.uri);
|
||||
// return languageService.format(document, null, formatParams.options);
|
||||
// });
|
||||
function merge(src: any, dst: any) : any {
|
||||
for (var key in src) {
|
||||
if (src.hasOwnProperty(key)) {
|
||||
dst[key] = src[key];
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
// connection.onDocumentRangeFormatting(formatParams => {
|
||||
// let document = documents.get(formatParams.textDocument.uri);
|
||||
// return languageService.format(document, formatParams.range, formatParams.options);
|
||||
// });
|
||||
function getFormattingOptions(formatParams : any) {
|
||||
let formatSettings = languageSettings && languageSettings.format;
|
||||
if (!formatSettings) {
|
||||
return formatParams;
|
||||
}
|
||||
return merge(formatParams, merge(formatSettings, {}));
|
||||
}
|
||||
|
||||
connection.onDocumentFormatting(formatParams => {
|
||||
let document = documents.get(formatParams.textDocument.uri);
|
||||
return languageService.format(document, null, getFormattingOptions(formatParams));
|
||||
});
|
||||
|
||||
connection.onDocumentRangeFormatting(formatParams => {
|
||||
let document = documents.get(formatParams.textDocument.uri);
|
||||
return languageService.format(document, formatParams.range, getFormattingOptions(formatParams));
|
||||
});
|
||||
|
||||
// Listen on the connection
|
||||
connection.listen();
|
|
@ -5,12 +5,15 @@
|
|||
'use strict';
|
||||
import { parse} from './parser/htmlParser';
|
||||
import { doComplete } from './services/htmlCompletion';
|
||||
import { format } from './services/htmlFormatter';
|
||||
import { TextDocument, Position, CompletionItem, CompletionList, Hover, Range, SymbolInformation, Diagnostic, TextEdit, FormattingOptions, MarkedString } from 'vscode-languageserver-types';
|
||||
|
||||
export { TextDocument, Position, CompletionItem, CompletionList, Hover, Range, SymbolInformation, Diagnostic, TextEdit, FormattingOptions, MarkedString };
|
||||
|
||||
|
||||
export interface HTMLFormatConfiguration {
|
||||
tabSize: number;
|
||||
insertSpaces: boolean;
|
||||
wrapLineLength: number;
|
||||
unformatted: string;
|
||||
indentInnerHtml: boolean;
|
||||
|
@ -37,7 +40,7 @@ export interface LanguageService {
|
|||
doComplete(document: TextDocument, position: Position, doc: HTMLDocument): CompletionList;
|
||||
// findDocumentSymbols(document: TextDocument, doc: HTMLDocument): SymbolInformation[];
|
||||
// doHover(document: TextDocument, position: Position, doc: HTMLDocument): Hover;
|
||||
// format(document: TextDocument, range: Range, options: FormattingOptions): TextEdit[];
|
||||
format(document: TextDocument, range: Range, options: HTMLFormatConfiguration): TextEdit[];
|
||||
}
|
||||
|
||||
export function getLanguageService() : LanguageService {
|
||||
|
@ -46,6 +49,7 @@ export function getLanguageService() : LanguageService {
|
|||
|
||||
configure: (settings) => {},
|
||||
parseHTMLDocument: (document) => parse(document.getText()),
|
||||
doComplete
|
||||
doComplete,
|
||||
format
|
||||
};
|
||||
}
|
9
extensions/html/server/src/service/lib/OSSREADME.json
Normal file
9
extensions/html/server/src/service/lib/OSSREADME.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
// June 2012
|
||||
// ATTENTION - THIS DIRECTORY CONTAINS THIRD PARTY OPEN SOURCE MATERIALS: THEY ARE CLEARED ONLY FOR LIMITED USE BY MONACO FOR THE MONACO PRODUCT. DO NOT USE OR SHARE THIS CODE WITHOUT APPROVAL PURSUANT TO THE MICROSOFT OPEN SOURCE SOFTWARE APPROVAL POLICY.APPROVAL
|
||||
|
||||
[{
|
||||
"name": "js-beautify",
|
||||
"version": "1.6.2",
|
||||
"license": "MIT",
|
||||
"repositoryURL": "https://github.com/beautify-web/js-beautify"
|
||||
}]
|
18
extensions/html/server/src/service/lib/beautify-css.d.ts
vendored
Normal file
18
extensions/html/server/src/service/lib/beautify-css.d.ts
vendored
Normal file
|
@ -0,0 +1,18 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
export interface IBeautifyCSSOptions {
|
||||
indent_size?: number; // (4) — indentation size,
|
||||
indent_char?: string; // (space) — character to indent with,
|
||||
selector_separator_newline?: boolean; // (true) - separate selectors with newline or not (e.g. "a,\nbr" or "a, br")
|
||||
end_with_newline?: boolean; // (false) - end with a newline
|
||||
newline_between_rules?: boolean; // (true) - add a new line after every css rule
|
||||
}
|
||||
|
||||
export interface IBeautifyCSS {
|
||||
(value:string, options:IBeautifyCSSOptions): string;
|
||||
}
|
||||
|
||||
export declare var css_beautify:IBeautifyCSS;
|
493
extensions/html/server/src/service/lib/beautify-css.js
Normal file
493
extensions/html/server/src/service/lib/beautify-css.js
Normal file
|
@ -0,0 +1,493 @@
|
|||
// copied https://raw.githubusercontent.com/beautify-web/js-beautify/master/js/lib/beautify-css.js
|
||||
|
||||
/*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
|
||||
/*
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2007-2013 Einar Lielmanis and contributors.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation files
|
||||
(the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
|
||||
CSS Beautifier
|
||||
---------------
|
||||
|
||||
Written by Harutyun Amirjanyan, (amirjanyan@gmail.com)
|
||||
|
||||
Based on code initially developed by: Einar Lielmanis, <einar@jsbeautifier.org>
|
||||
http://jsbeautifier.org/
|
||||
|
||||
Usage:
|
||||
css_beautify(source_text);
|
||||
css_beautify(source_text, options);
|
||||
|
||||
The options are (default in brackets):
|
||||
indent_size (4) — indentation size,
|
||||
indent_char (space) — character to indent with,
|
||||
selector_separator_newline (true) - separate selectors with newline or
|
||||
not (e.g. "a,\nbr" or "a, br")
|
||||
end_with_newline (false) - end with a newline
|
||||
newline_between_rules (true) - add a new line after every css rule
|
||||
|
||||
e.g
|
||||
|
||||
css_beautify(css_source_text, {
|
||||
'indent_size': 1,
|
||||
'indent_char': '\t',
|
||||
'selector_separator': ' ',
|
||||
'end_with_newline': false,
|
||||
'newline_between_rules': true
|
||||
});
|
||||
*/
|
||||
|
||||
// http://www.w3.org/TR/CSS21/syndata.html#tokenization
|
||||
// http://www.w3.org/TR/css3-syntax/
|
||||
|
||||
(function() {
|
||||
function css_beautify(source_text, options) {
|
||||
options = options || {};
|
||||
source_text = source_text || '';
|
||||
// HACK: newline parsing inconsistent. This brute force normalizes the input.
|
||||
source_text = source_text.replace(/\r\n|[\r\u2028\u2029]/g, '\n')
|
||||
|
||||
var indentSize = options.indent_size || 4;
|
||||
var indentCharacter = options.indent_char || ' ';
|
||||
var selectorSeparatorNewline = (options.selector_separator_newline === undefined) ? true : options.selector_separator_newline;
|
||||
var end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline;
|
||||
var newline_between_rules = (options.newline_between_rules === undefined) ? true : options.newline_between_rules;
|
||||
var eol = options.eol ? options.eol : '\n';
|
||||
|
||||
// compatibility
|
||||
if (typeof indentSize === "string") {
|
||||
indentSize = parseInt(indentSize, 10);
|
||||
}
|
||||
|
||||
if(options.indent_with_tabs){
|
||||
indentCharacter = '\t';
|
||||
indentSize = 1;
|
||||
}
|
||||
|
||||
eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n')
|
||||
|
||||
|
||||
// tokenizer
|
||||
var whiteRe = /^\s+$/;
|
||||
var wordRe = /[\w$\-_]/;
|
||||
|
||||
var pos = -1,
|
||||
ch;
|
||||
var parenLevel = 0;
|
||||
|
||||
function next() {
|
||||
ch = source_text.charAt(++pos);
|
||||
return ch || '';
|
||||
}
|
||||
|
||||
function peek(skipWhitespace) {
|
||||
var result = '';
|
||||
var prev_pos = pos;
|
||||
if (skipWhitespace) {
|
||||
eatWhitespace();
|
||||
}
|
||||
result = source_text.charAt(pos + 1) || '';
|
||||
pos = prev_pos - 1;
|
||||
next();
|
||||
return result;
|
||||
}
|
||||
|
||||
function eatString(endChars) {
|
||||
var start = pos;
|
||||
while (next()) {
|
||||
if (ch === "\\") {
|
||||
next();
|
||||
} else if (endChars.indexOf(ch) !== -1) {
|
||||
break;
|
||||
} else if (ch === "\n") {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return source_text.substring(start, pos + 1);
|
||||
}
|
||||
|
||||
function peekString(endChar) {
|
||||
var prev_pos = pos;
|
||||
var str = eatString(endChar);
|
||||
pos = prev_pos - 1;
|
||||
next();
|
||||
return str;
|
||||
}
|
||||
|
||||
function eatWhitespace() {
|
||||
var result = '';
|
||||
while (whiteRe.test(peek())) {
|
||||
next();
|
||||
result += ch;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function skipWhitespace() {
|
||||
var result = '';
|
||||
if (ch && whiteRe.test(ch)) {
|
||||
result = ch;
|
||||
}
|
||||
while (whiteRe.test(next())) {
|
||||
result += ch;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function eatComment(singleLine) {
|
||||
var start = pos;
|
||||
singleLine = peek() === "/";
|
||||
next();
|
||||
while (next()) {
|
||||
if (!singleLine && ch === "*" && peek() === "/") {
|
||||
next();
|
||||
break;
|
||||
} else if (singleLine && ch === "\n") {
|
||||
return source_text.substring(start, pos);
|
||||
}
|
||||
}
|
||||
|
||||
return source_text.substring(start, pos) + ch;
|
||||
}
|
||||
|
||||
|
||||
function lookBack(str) {
|
||||
return source_text.substring(pos - str.length, pos).toLowerCase() ===
|
||||
str;
|
||||
}
|
||||
|
||||
// Nested pseudo-class if we are insideRule
|
||||
// and the next special character found opens
|
||||
// a new block
|
||||
function foundNestedPseudoClass() {
|
||||
var openParen = 0;
|
||||
for (var i = pos + 1; i < source_text.length; i++) {
|
||||
var ch = source_text.charAt(i);
|
||||
if (ch === "{") {
|
||||
return true;
|
||||
} else if (ch === '(') {
|
||||
// pseudoclasses can contain ()
|
||||
openParen += 1;
|
||||
} else if (ch === ')') {
|
||||
if (openParen == 0) {
|
||||
return false;
|
||||
}
|
||||
openParen -= 1;
|
||||
} else if (ch === ";" || ch === "}") {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// printer
|
||||
var basebaseIndentString = source_text.match(/^[\t ]*/)[0];
|
||||
var singleIndent = new Array(indentSize + 1).join(indentCharacter);
|
||||
var indentLevel = 0;
|
||||
var nestedLevel = 0;
|
||||
|
||||
function indent() {
|
||||
indentLevel++;
|
||||
basebaseIndentString += singleIndent;
|
||||
}
|
||||
|
||||
function outdent() {
|
||||
indentLevel--;
|
||||
basebaseIndentString = basebaseIndentString.slice(0, -indentSize);
|
||||
}
|
||||
|
||||
var print = {};
|
||||
print["{"] = function(ch) {
|
||||
print.singleSpace();
|
||||
output.push(ch);
|
||||
print.newLine();
|
||||
};
|
||||
print["}"] = function(ch) {
|
||||
print.newLine();
|
||||
output.push(ch);
|
||||
print.newLine();
|
||||
};
|
||||
|
||||
print._lastCharWhitespace = function() {
|
||||
return whiteRe.test(output[output.length - 1]);
|
||||
};
|
||||
|
||||
print.newLine = function(keepWhitespace) {
|
||||
if (output.length) {
|
||||
if (!keepWhitespace && output[output.length - 1] !== '\n') {
|
||||
print.trim();
|
||||
}
|
||||
|
||||
output.push('\n');
|
||||
|
||||
if (basebaseIndentString) {
|
||||
output.push(basebaseIndentString);
|
||||
}
|
||||
}
|
||||
};
|
||||
print.singleSpace = function() {
|
||||
if (output.length && !print._lastCharWhitespace()) {
|
||||
output.push(' ');
|
||||
}
|
||||
};
|
||||
|
||||
print.preserveSingleSpace = function() {
|
||||
if (isAfterSpace) {
|
||||
print.singleSpace();
|
||||
}
|
||||
};
|
||||
|
||||
print.trim = function() {
|
||||
while (print._lastCharWhitespace()) {
|
||||
output.pop();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
var output = [];
|
||||
/*_____________________--------------------_____________________*/
|
||||
|
||||
var insideRule = false;
|
||||
var insidePropertyValue = false;
|
||||
var enteringConditionalGroup = false;
|
||||
var top_ch = '';
|
||||
var last_top_ch = '';
|
||||
|
||||
while (true) {
|
||||
var whitespace = skipWhitespace();
|
||||
var isAfterSpace = whitespace !== '';
|
||||
var isAfterNewline = whitespace.indexOf('\n') !== -1;
|
||||
last_top_ch = top_ch;
|
||||
top_ch = ch;
|
||||
|
||||
if (!ch) {
|
||||
break;
|
||||
} else if (ch === '/' && peek() === '*') { /* css comment */
|
||||
var header = indentLevel === 0;
|
||||
|
||||
if (isAfterNewline || header) {
|
||||
print.newLine();
|
||||
}
|
||||
|
||||
output.push(eatComment());
|
||||
print.newLine();
|
||||
if (header) {
|
||||
print.newLine(true);
|
||||
}
|
||||
} else if (ch === '/' && peek() === '/') { // single line comment
|
||||
if (!isAfterNewline && last_top_ch !== '{' ) {
|
||||
print.trim();
|
||||
}
|
||||
print.singleSpace();
|
||||
output.push(eatComment());
|
||||
print.newLine();
|
||||
} else if (ch === '@') {
|
||||
print.preserveSingleSpace();
|
||||
output.push(ch);
|
||||
|
||||
// strip trailing space, if present, for hash property checks
|
||||
var variableOrRule = peekString(": ,;{}()[]/='\"");
|
||||
|
||||
if (variableOrRule.match(/[ :]$/)) {
|
||||
// we have a variable or pseudo-class, add it and insert one space before continuing
|
||||
next();
|
||||
variableOrRule = eatString(": ").replace(/\s$/, '');
|
||||
output.push(variableOrRule);
|
||||
print.singleSpace();
|
||||
}
|
||||
|
||||
variableOrRule = variableOrRule.replace(/\s$/, '')
|
||||
|
||||
// might be a nesting at-rule
|
||||
if (variableOrRule in css_beautify.NESTED_AT_RULE) {
|
||||
nestedLevel += 1;
|
||||
if (variableOrRule in css_beautify.CONDITIONAL_GROUP_RULE) {
|
||||
enteringConditionalGroup = true;
|
||||
}
|
||||
}
|
||||
} else if (ch === '#' && peek() === '{') {
|
||||
print.preserveSingleSpace();
|
||||
output.push(eatString('}'));
|
||||
} else if (ch === '{') {
|
||||
if (peek(true) === '}') {
|
||||
eatWhitespace();
|
||||
next();
|
||||
print.singleSpace();
|
||||
output.push("{}");
|
||||
print.newLine();
|
||||
if (newline_between_rules && indentLevel === 0) {
|
||||
print.newLine(true);
|
||||
}
|
||||
} else {
|
||||
indent();
|
||||
print["{"](ch);
|
||||
// when entering conditional groups, only rulesets are allowed
|
||||
if (enteringConditionalGroup) {
|
||||
enteringConditionalGroup = false;
|
||||
insideRule = (indentLevel > nestedLevel);
|
||||
} else {
|
||||
// otherwise, declarations are also allowed
|
||||
insideRule = (indentLevel >= nestedLevel);
|
||||
}
|
||||
}
|
||||
} else if (ch === '}') {
|
||||
outdent();
|
||||
print["}"](ch);
|
||||
insideRule = false;
|
||||
insidePropertyValue = false;
|
||||
if (nestedLevel) {
|
||||
nestedLevel--;
|
||||
}
|
||||
if (newline_between_rules && indentLevel === 0) {
|
||||
print.newLine(true);
|
||||
}
|
||||
} else if (ch === ":") {
|
||||
eatWhitespace();
|
||||
if ((insideRule || enteringConditionalGroup) &&
|
||||
!(lookBack("&") || foundNestedPseudoClass())) {
|
||||
// 'property: value' delimiter
|
||||
// which could be in a conditional group query
|
||||
insidePropertyValue = true;
|
||||
output.push(':');
|
||||
print.singleSpace();
|
||||
} else {
|
||||
// sass/less parent reference don't use a space
|
||||
// sass nested pseudo-class don't use a space
|
||||
if (peek() === ":") {
|
||||
// pseudo-element
|
||||
next();
|
||||
output.push("::");
|
||||
} else {
|
||||
// pseudo-class
|
||||
output.push(':');
|
||||
}
|
||||
}
|
||||
} else if (ch === '"' || ch === '\'') {
|
||||
print.preserveSingleSpace();
|
||||
output.push(eatString(ch));
|
||||
} else if (ch === ';') {
|
||||
insidePropertyValue = false;
|
||||
output.push(ch);
|
||||
print.newLine();
|
||||
} else if (ch === '(') { // may be a url
|
||||
if (lookBack("url")) {
|
||||
output.push(ch);
|
||||
eatWhitespace();
|
||||
if (next()) {
|
||||
if (ch !== ')' && ch !== '"' && ch !== '\'') {
|
||||
output.push(eatString(')'));
|
||||
} else {
|
||||
pos--;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
parenLevel++;
|
||||
print.preserveSingleSpace();
|
||||
output.push(ch);
|
||||
eatWhitespace();
|
||||
}
|
||||
} else if (ch === ')') {
|
||||
output.push(ch);
|
||||
parenLevel--;
|
||||
} else if (ch === ',') {
|
||||
output.push(ch);
|
||||
eatWhitespace();
|
||||
if (selectorSeparatorNewline && !insidePropertyValue && parenLevel < 1) {
|
||||
print.newLine();
|
||||
} else {
|
||||
print.singleSpace();
|
||||
}
|
||||
} else if (ch === ']') {
|
||||
output.push(ch);
|
||||
} else if (ch === '[') {
|
||||
print.preserveSingleSpace();
|
||||
output.push(ch);
|
||||
} else if (ch === '=') { // no whitespace before or after
|
||||
eatWhitespace()
|
||||
ch = '=';
|
||||
output.push(ch);
|
||||
} else {
|
||||
print.preserveSingleSpace();
|
||||
output.push(ch);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var sweetCode = '';
|
||||
if (basebaseIndentString) {
|
||||
sweetCode += basebaseIndentString;
|
||||
}
|
||||
|
||||
sweetCode += output.join('').replace(/[\r\n\t ]+$/, '');
|
||||
|
||||
// establish end_with_newline
|
||||
if (end_with_newline) {
|
||||
sweetCode += '\n';
|
||||
}
|
||||
|
||||
if (eol != '\n') {
|
||||
sweetCode = sweetCode.replace(/[\n]/g, eol);
|
||||
}
|
||||
|
||||
return sweetCode;
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/CSS/At-rule
|
||||
css_beautify.NESTED_AT_RULE = {
|
||||
"@page": true,
|
||||
"@font-face": true,
|
||||
"@keyframes": true,
|
||||
// also in CONDITIONAL_GROUP_RULE below
|
||||
"@media": true,
|
||||
"@supports": true,
|
||||
"@document": true
|
||||
};
|
||||
css_beautify.CONDITIONAL_GROUP_RULE = {
|
||||
"@media": true,
|
||||
"@supports": true,
|
||||
"@document": true
|
||||
};
|
||||
|
||||
/*global define */
|
||||
if (typeof define === "function" && define.amd) {
|
||||
// Add support for AMD ( https://github.com/amdjs/amdjs-api/wiki/AMD#defineamd-property- )
|
||||
define([], function() {
|
||||
return {
|
||||
css_beautify: css_beautify
|
||||
};
|
||||
});
|
||||
} else if (typeof exports !== "undefined") {
|
||||
// Add support for CommonJS. Just put this file somewhere on your require.paths
|
||||
// and you will be able to `var html_beautify = require("beautify").html_beautify`.
|
||||
exports.css_beautify = css_beautify;
|
||||
} else if (typeof window !== "undefined") {
|
||||
// If we're running a web page and don't have either of the above, add our one global
|
||||
window.css_beautify = css_beautify;
|
||||
} else if (typeof global !== "undefined") {
|
||||
// If we don't even have window, try global.
|
||||
global.css_beautify = css_beautify;
|
||||
}
|
||||
|
||||
}());
|
85
extensions/html/server/src/service/lib/beautify-html.d.ts
vendored
Normal file
85
extensions/html/server/src/service/lib/beautify-html.d.ts
vendored
Normal file
|
@ -0,0 +1,85 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
export interface IBeautifyHTMLOptions {
|
||||
/**
|
||||
* indent <head> and <body> sections
|
||||
* default false
|
||||
*/
|
||||
indent_inner_html?: boolean;
|
||||
|
||||
/**
|
||||
* indentation size
|
||||
* default 4
|
||||
*/
|
||||
indent_size?: number; // indentation size,
|
||||
|
||||
/**
|
||||
* character to indent with
|
||||
* default space
|
||||
*/
|
||||
indent_char?: string; // character to indent with,
|
||||
|
||||
/**
|
||||
* maximum amount of characters per line (0 = disable)
|
||||
* default 250
|
||||
*/
|
||||
wrap_line_length?: number;
|
||||
|
||||
/**
|
||||
* put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line, or attempt to keep them where they are.
|
||||
* "collapse" | "expand" | "end-expand" | "none"
|
||||
* default "collapse"
|
||||
*/
|
||||
brace_style?: string;
|
||||
|
||||
/**
|
||||
* list of tags, that shouldn't be reformatted
|
||||
* defaults to inline tags
|
||||
*/
|
||||
unformatted?: string[];
|
||||
|
||||
/**
|
||||
* "keep"|"separate"|"normal"
|
||||
* default normal
|
||||
*/
|
||||
indent_scripts?: string;
|
||||
|
||||
/**
|
||||
* whether existing line breaks before elements should be preserved. Only works before elements, not inside tags or for text.
|
||||
* default true
|
||||
*/
|
||||
preserve_newlines?: boolean;
|
||||
|
||||
/**
|
||||
* maximum number of line breaks to be preserved in one chunk
|
||||
* default unlimited
|
||||
*/
|
||||
max_preserve_newlines?: number;
|
||||
|
||||
/**
|
||||
* format and indent {{#foo}} and {{/foo}}
|
||||
* default false
|
||||
*/
|
||||
indent_handlebars?: boolean;
|
||||
|
||||
/**
|
||||
* end with a newline
|
||||
* default false
|
||||
*/
|
||||
end_with_newline?: boolean;
|
||||
|
||||
/**
|
||||
* List of tags that should have an extra newline before them.
|
||||
* default [head,body,/html]
|
||||
*/
|
||||
extra_liners?: string[];
|
||||
}
|
||||
|
||||
export interface IBeautifyHTML {
|
||||
(value:string, options:IBeautifyHTMLOptions): string;
|
||||
}
|
||||
|
||||
export declare var html_beautify:IBeautifyHTML;
|
1023
extensions/html/server/src/service/lib/beautify-html.js
Normal file
1023
extensions/html/server/src/service/lib/beautify-html.js
Normal file
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,9 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2007-2013 Einar Lielmanis and contributors.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
11
extensions/html/server/src/service/lib/beautify.ts
Normal file
11
extensions/html/server/src/service/lib/beautify.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
/*
|
||||
* Mock for the JS formatter. Ignore formatting of JS content in HTML.
|
||||
*/
|
||||
export function js_beautify(js_source_text: string, options: any) {
|
||||
// no formatting
|
||||
return js_source_text;
|
||||
}
|
57
extensions/html/server/src/service/services/htmlFormatter.ts
Normal file
57
extensions/html/server/src/service/services/htmlFormatter.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import {HTMLFormatConfiguration} from '../htmlLanguageService';
|
||||
import {TextDocument, Range, TextEdit} from 'vscode-languageserver-types';
|
||||
import {IBeautifyHTMLOptions, html_beautify} from '../lib/beautify-html';
|
||||
|
||||
export function format(document: TextDocument, range: Range, options: HTMLFormatConfiguration): TextEdit[] {
|
||||
let value = document.getText();
|
||||
if (range) {
|
||||
let startOffset = document.offsetAt(range.start);
|
||||
let endOffset = document.offsetAt(range.end);
|
||||
value = value.substring(startOffset, endOffset);
|
||||
}
|
||||
let htmlOptions : IBeautifyHTMLOptions = {
|
||||
indent_size: options.insertSpaces ? options.tabSize : 1,
|
||||
indent_char: options.insertSpaces ? ' ' : '\t',
|
||||
wrap_line_length: getFormatOption(options, 'wrapLineLength', 120),
|
||||
unformatted: getTagsFormatOption(options, 'unformatted', void 0),
|
||||
indent_inner_html: getFormatOption(options, 'indentInnerHtml', false),
|
||||
preserve_newlines: getFormatOption(options, 'preserveNewLines', false),
|
||||
max_preserve_newlines: getFormatOption(options, 'maxPreserveNewLines', void 0),
|
||||
indent_handlebars: getFormatOption(options, 'indentHandlebars', false),
|
||||
end_with_newline: getFormatOption(options, 'endWithNewline', false),
|
||||
extra_liners: getTagsFormatOption(options, 'extraLiners', void 0),
|
||||
};
|
||||
|
||||
let result = html_beautify(value, htmlOptions);
|
||||
return [{
|
||||
range: range,
|
||||
newText: result
|
||||
}];
|
||||
}
|
||||
|
||||
function getFormatOption(options: HTMLFormatConfiguration, key: string, dflt: any): any {
|
||||
if (options && options.hasOwnProperty(key)) {
|
||||
let value = options[key];
|
||||
if (value !== null) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return dflt;
|
||||
}
|
||||
|
||||
function getTagsFormatOption(options: HTMLFormatConfiguration, key: string, dflt: string[]): string[] {
|
||||
let list = <string> getFormatOption(options, key, null);
|
||||
if (typeof list === 'string') {
|
||||
if (list.length > 0) {
|
||||
return list.split(',').map(t => t.trim().toLowerCase());
|
||||
}
|
||||
return [];
|
||||
}
|
||||
return dflt;
|
||||
}
|
Loading…
Reference in a new issue