diff --git a/src/Extension.ts b/src/Extension.ts index e54d51e..379655b 100644 --- a/src/Extension.ts +++ b/src/Extension.ts @@ -51,6 +51,10 @@ export function deactivate() { } function _handleContextChange(editor: vsc.TextEditor): void { + if (!editor || !editor.document) { + return; + } + switch (editor.document.languageId) { case 'xquery': XQueryLintingFeatureProvider.provideXQueryDiagnostics(editor); diff --git a/src/services/XmlFormatter.ts b/src/services/XmlFormatter.ts index 2aed3c1..a615acd 100644 --- a/src/services/XmlFormatter.ts +++ b/src/services/XmlFormatter.ts @@ -119,8 +119,10 @@ export class XmlFormatter { removeComments = false; } + xml = this._stripLineBreaks(xml); xml = (removeComments) ? xml.replace(/\/g, '') : xml; xml = xml.replace(/>\s{0,}<'); + xml = xml.replace(/"\s+(?=[^\s]+=)/g, '" '); return xml; } @@ -130,6 +132,34 @@ export class XmlFormatter { return `${this.newLine}${this.indentPattern.repeat(level)}${trailingValue}`; } + + private _stripLineBreaks(xml: string): string { + let output: string = ''; + let inTag: boolean = false; + let inTagName: boolean = false; + let inCdata: boolean = false; + let inAttribute: boolean = false; + + for (let i = 0; i < xml.length; i++) { + let char: string = xml.charAt(i); + + if (char == '!' && (xml.substr(i, 8) == '![CDATA[' || xml.substr(i, 3) == '!--')) { + inCdata = true; + } + + else if (char == ']' && (xml.substr(i, 3) == ']]>' || xml.substr(i, 3) == '-->')) { + inCdata = false; + } + + else if (char.search(/[\r\n]/g) > -1 && !inCdata) { + continue; + } + + output += char; + } + + return output; + } } export interface IXmlFormatterOptions {