Add XML Tree View

This commit is contained in:
Josh Johnson 2018-04-27 23:10:55 -04:00
parent dab5b0c072
commit c37ac45e80
11 changed files with 388 additions and 12 deletions

29
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "xml",
"version": "2.0.0",
"version": "2.0.0-preview.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -16,6 +16,12 @@
"integrity": "sha512-jjpyQsKGsOF/wUElNjfPULk+d8PKvJOIXk3IUeBYYmNCy5dMWfrI+JiixYNw8ppKOlcRwWTXFl0B+i5oGrf95Q==",
"dev": true
},
"@types/xmldom": {
"version": "0.1.29",
"resolved": "https://registry.npmjs.org/@types/xmldom/-/xmldom-0.1.29.tgz",
"integrity": "sha1-xEKLDKhtO4gUdXJv2UmAs4onw4E=",
"dev": true
},
"ajv": {
"version": "5.5.2",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-5.5.2.tgz",
@ -2616,14 +2622,6 @@
"integrity": "sha1-l+mNj6TRBdYqJpHR3AfoINuN/E8=",
"dev": true
},
"string_decoder": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz",
"integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==",
"requires": {
"safe-buffer": "5.1.1"
}
},
"string-length": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/string-length/-/string-length-1.0.1.tgz",
@ -2632,6 +2630,14 @@
"strip-ansi": "3.0.1"
}
},
"string_decoder": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz",
"integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==",
"requires": {
"safe-buffer": "5.1.1"
}
},
"stringstream": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.5.tgz",
@ -3067,6 +3073,11 @@
"user-home": "1.1.1"
}
},
"xmldom": {
"version": "0.1.27",
"resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.1.27.tgz",
"integrity": "sha1-1QH5ezvbQDr4757MIFcxh6rawOk="
},
"xqlint": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/xqlint/-/xqlint-0.4.0.tgz",

View File

@ -63,6 +63,12 @@
"title": "XML Tools Configuration",
"type": "object",
"properties": {
"xmlTools.enableXmlTreeView": {
"type": "boolean",
"default": true,
"description": "Enables the XML Document view in the explorer for XML documents.",
"scope": "window"
},
"xmlTools.ignoreDefaultNamespace": {
"type": "boolean",
"default": true,
@ -153,7 +159,16 @@
],
"configuration": "./languages/xquery/xquery.json"
}
]
],
"views": {
"explorer": [
{
"id": "xmlTreeView",
"name": "XML Document",
"when": "xmlTreeViewEnabled"
}
]
}
},
"scripts": {
"vscode:prepublish": "npm run compile",
@ -166,11 +181,13 @@
"devDependencies": {
"@types/mocha": "^2.2.42",
"@types/node": "^7.0.43",
"@types/xmldom": "^0.1.29",
"tslint": "^5.9.1",
"typescript": "^2.6.1",
"vscode": "^1.1.6"
},
"dependencies": {
"xmldom": "^0.1.27",
"xqlint": "^0.4.0"
}
}

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 490 490" style="enable-background:new 0 0 490 490;" xml:space="preserve" width="512px" height="512px">
<path d="M374.621,429.894c-30.535,14.94-68.393,22.412-113.58,22.412c-42.256,0-80.758-7.175-115.517-21.519 c-34.759-14.35-61.315-36.548-79.682-66.598c-18.367-30.056-27.555-65.878-27.555-107.479c0-39.729,9.188-76.947,27.555-111.675 c18.367-34.716,44.239-61.375,77.61-79.967c33.38-18.592,70.24-27.889,110.609-27.889c35.355,0,67.65,7.39,96.885,22.157 c29.236,14.773,51.219,34.472,65.969,59.086c14.75,24.619,22.12,51.105,22.12,79.464c0,24.277-5.737,47.837-17.203,70.671 c-11.475,22.835-27.036,41.045-46.694,54.629c-14.838,10.357-26.821,15.532-35.97,15.532c-5.005,0-9.267-1.651-12.805-4.966 c-3.538-3.309-5.297-7.256-5.297-11.84c0-2.544,2.581-15.7,7.761-39.474l37.25-170.889h-46.311l-8.797,39.474 c-9.824-15.787-21.553-27.546-35.179-35.272c-13.626-7.725-27.945-11.591-42.949-11.591c-19.657,0-39.93,6.792-60.798,20.377 c-20.869,13.584-38.336,33.916-52.392,60.993c-14.055,27.083-21.084,54.119-21.084,81.115c0,21.901,4.526,42.023,13.587,60.361 c9.051,18.337,20.819,31.922,35.315,40.749c14.486,8.826,29.402,13.243,44.758,13.243c13.968,0,27.897-3.86,41.777-11.592 c13.88-7.72,26.43-17.955,37.642-30.688c1.036,10.357,2.679,17.659,4.917,21.901c3.451,6.625,8.836,11.719,16.167,15.284 c7.331,3.565,16.949,5.35,28.855,5.35c37.25,0,71.57-17.15,102.965-51.448c35.189-38.199,52.773-81.915,52.773-131.16 c0-34.461-8.366-66.383-25.091-95.755c-19.667-34.131-46.527-59.805-80.592-77.041C337.575,8.618,299.161,0,256.387,0 c-55.197,0-102.154,12.223-140.871,36.668C76.79,61.12,46.909,96.352,25.873,142.363C8.621,179.889,0,219.363,0,260.79 c0,37.178,7.243,71.818,21.728,103.903c11.554,25.298,28.718,47.668,51.484,67.113c22.774,19.439,50.319,33.997,82.663,43.676 C188.21,485.161,223.526,490,261.822,490c41.562,0,77.092-5.396,106.582-16.176c29.5-10.785,54.934-25.425,76.32-43.93 c21.387-18.506,36.478-36.675,45.276-54.502h-46.821C427.999,396.784,405.147,414.953,374.621,429.894z M286.913,266.771 c-6.207,17.237-14.193,31.453-23.928,42.662c-9.745,11.203-19.97,19.653-30.663,25.338c-10.693,5.692-20.869,8.531-30.526,8.531 c-14.485,0-27.339-6.45-38.55-19.351c-11.212-12.901-16.813-30.983-16.813-54.247c0-14.605,2.756-30.648,8.279-48.139 c5.513-17.484,12.844-32.345,21.993-44.567c9.139-12.224,18.582-21.097,28.327-26.614c9.745-5.517,20.136-8.276,31.171-8.276 c16.734,0,30.917,6.369,42.559,19.102c11.641,12.733,17.467,30.646,17.467,53.737C296.228,232.264,293.121,249.54,286.913,266.771z" fill="#FFFFFF"/>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 490 490" style="enable-background:new 0 0 490 490;" xml:space="preserve">
<path d="M374.621,429.894c-30.535,14.94-68.393,22.412-113.58,22.412c-42.256,0-80.758-7.175-115.517-21.519
c-34.759-14.35-61.315-36.548-79.682-66.598c-18.367-30.056-27.555-65.878-27.555-107.479c0-39.729,9.188-76.947,27.555-111.675
c18.367-34.716,44.239-61.375,77.61-79.967c33.38-18.592,70.24-27.889,110.609-27.889c35.355,0,67.65,7.39,96.885,22.157
c29.236,14.773,51.219,34.472,65.969,59.086c14.75,24.619,22.12,51.105,22.12,79.464c0,24.277-5.737,47.837-17.203,70.671
c-11.475,22.835-27.036,41.045-46.694,54.629c-14.838,10.357-26.821,15.532-35.97,15.532c-5.005,0-9.267-1.651-12.805-4.966
c-3.538-3.309-5.297-7.256-5.297-11.84c0-2.544,2.581-15.7,7.761-39.474l37.25-170.889h-46.311l-8.797,39.474
c-9.824-15.787-21.553-27.546-35.179-35.272c-13.626-7.725-27.945-11.591-42.949-11.591c-19.657,0-39.93,6.792-60.798,20.377
c-20.869,13.584-38.336,33.916-52.392,60.993c-14.055,27.083-21.084,54.119-21.084,81.115c0,21.901,4.526,42.023,13.587,60.361
c9.051,18.337,20.819,31.922,35.315,40.749c14.486,8.826,29.402,13.243,44.758,13.243c13.968,0,27.897-3.86,41.777-11.592
c13.88-7.72,26.43-17.955,37.642-30.688c1.036,10.357,2.679,17.659,4.917,21.901c3.451,6.625,8.836,11.719,16.167,15.284
c7.331,3.565,16.949,5.35,28.855,5.35c37.25,0,71.57-17.15,102.965-51.448c35.189-38.199,52.773-81.915,52.773-131.16
c0-34.461-8.366-66.383-25.091-95.755c-19.667-34.131-46.527-59.805-80.592-77.041C337.575,8.618,299.161,0,256.387,0
c-55.197,0-102.154,12.223-140.871,36.668C76.79,61.12,46.909,96.352,25.873,142.363C8.621,179.889,0,219.363,0,260.79
c0,37.178,7.243,71.818,21.728,103.903c11.554,25.298,28.718,47.668,51.484,67.113c22.774,19.439,50.319,33.997,82.663,43.676
C188.21,485.161,223.526,490,261.822,490c41.562,0,77.092-5.396,106.582-16.176c29.5-10.785,54.934-25.425,76.32-43.93
c21.387-18.506,36.478-36.675,45.276-54.502h-46.821C427.999,396.784,405.147,414.953,374.621,429.894z M286.913,266.771
c-6.207,17.237-14.193,31.453-23.928,42.662c-9.745,11.203-19.97,19.653-30.663,25.338c-10.693,5.692-20.869,8.531-30.526,8.531
c-14.485,0-27.339-6.45-38.55-19.351c-11.212-12.901-16.813-30.983-16.813-54.247c0-14.605,2.756-30.648,8.279-48.139
c5.513-17.484,12.844-32.345,21.993-44.567c9.139-12.224,18.582-21.097,28.327-26.614c9.745-5.517,20.136-8.276,31.171-8.276
c16.734,0,30.917,6.369,42.559,19.102c11.641,12.733,17.467,30.646,17.467,53.737C296.228,232.264,293.121,249.54,286.913,266.771z"
/>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" width="512px" height="512px" viewBox="0 0 522.468 522.469" style="enable-background:new 0 0 522.468 522.469;" xml:space="preserve">
<g>
<g>
<path d="M325.762,70.513l-17.706-4.854c-2.279-0.76-4.524-0.521-6.707,0.715c-2.19,1.237-3.669,3.094-4.429,5.568L190.426,440.53 c-0.76,2.475-0.522,4.809,0.715,6.995c1.237,2.19,3.09,3.665,5.568,4.425l17.701,4.856c2.284,0.766,4.521,0.526,6.71-0.712 c2.19-1.243,3.666-3.094,4.425-5.564L332.042,81.936c0.759-2.474,0.523-4.808-0.716-6.999 C330.088,72.747,328.237,71.272,325.762,70.513z" fill="#FFFFFF"/>
<path d="M166.167,142.465c0-2.474-0.953-4.665-2.856-6.567l-14.277-14.276c-1.903-1.903-4.093-2.857-6.567-2.857 s-4.665,0.955-6.567,2.857L2.856,254.666C0.95,256.569,0,258.759,0,261.233c0,2.474,0.953,4.664,2.856,6.566l133.043,133.044 c1.902,1.906,4.089,2.854,6.567,2.854s4.665-0.951,6.567-2.854l14.277-14.268c1.903-1.902,2.856-4.093,2.856-6.57 c0-2.471-0.953-4.661-2.856-6.563L51.107,261.233l112.204-112.201C165.217,147.13,166.167,144.939,166.167,142.465z" fill="#FFFFFF"/>
<path d="M519.614,254.663L386.567,121.619c-1.902-1.902-4.093-2.857-6.563-2.857c-2.478,0-4.661,0.955-6.57,2.857l-14.271,14.275 c-1.902,1.903-2.851,4.09-2.851,6.567s0.948,4.665,2.851,6.567l112.206,112.204L359.163,373.442 c-1.902,1.902-2.851,4.093-2.851,6.563c0,2.478,0.948,4.668,2.851,6.57l14.271,14.268c1.909,1.906,4.093,2.854,6.57,2.854 c2.471,0,4.661-0.951,6.563-2.854L519.614,267.8c1.903-1.902,2.854-4.096,2.854-6.57 C522.468,258.755,521.517,256.565,519.614,254.663z" fill="#FFFFFF"/>
</g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="522.468px" height="522.469px" viewBox="0 0 522.468 522.469" style="enable-background:new 0 0 522.468 522.469;"
xml:space="preserve">
<g>
<g>
<path d="M325.762,70.513l-17.706-4.854c-2.279-0.76-4.524-0.521-6.707,0.715c-2.19,1.237-3.669,3.094-4.429,5.568L190.426,440.53
c-0.76,2.475-0.522,4.809,0.715,6.995c1.237,2.19,3.09,3.665,5.568,4.425l17.701,4.856c2.284,0.766,4.521,0.526,6.71-0.712
c2.19-1.243,3.666-3.094,4.425-5.564L332.042,81.936c0.759-2.474,0.523-4.808-0.716-6.999
C330.088,72.747,328.237,71.272,325.762,70.513z"/>
<path d="M166.167,142.465c0-2.474-0.953-4.665-2.856-6.567l-14.277-14.276c-1.903-1.903-4.093-2.857-6.567-2.857
s-4.665,0.955-6.567,2.857L2.856,254.666C0.95,256.569,0,258.759,0,261.233c0,2.474,0.953,4.664,2.856,6.566l133.043,133.044
c1.902,1.906,4.089,2.854,6.567,2.854s4.665-0.951,6.567-2.854l14.277-14.268c1.903-1.902,2.856-4.093,2.856-6.57
c0-2.471-0.953-4.661-2.856-6.563L51.107,261.233l112.204-112.201C165.217,147.13,166.167,144.939,166.167,142.465z"/>
<path d="M519.614,254.663L386.567,121.619c-1.902-1.902-4.093-2.857-6.563-2.857c-2.478,0-4.661,0.955-6.57,2.857l-14.271,14.275
c-1.902,1.903-2.851,4.09-2.851,6.567s0.948,4.665,2.851,6.567l112.206,112.204L359.163,373.442
c-1.902,1.902-2.851,4.093-2.851,6.563c0,2.478,0.948,4.668,2.851,6.57l14.271,14.268c1.909,1.906,4.093,2.854,6.57,2.854
c2.471,0,4.661-0.951,6.563-2.854L519.614,267.8c1.903-1.902,2.854-4.096,2.854-6.57
C522.468,258.755,521.517,256.565,519.614,254.663z"/>
</g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -1 +1,13 @@
export const extensionPrefix = "xmlTools";
export namespace commands {
export const setContext = "setContext";
}
export namespace contextKeys {
export const xmlTreeViewEnabled = "xmlTreeViewEnabled";
}
export namespace configKeys {
export const enableXmlTreeView = "enableXmlTreeView";
}

View File

@ -7,6 +7,7 @@ import { MinifyXmlCommandName, minifyXml } from "./formatting/commands/minifyXml
import { XmlFormatterFactory } from "./formatting/xml-formatter";
import { XmlFormattingEditProvider } from "./formatting/xml-formatting-edit-provider";
import { XQueryLinter } from "./linting/xquery-linter";
import { XmlTreeDataProvider } from "./tree-view/xml-tree-data-provider";
import * as constants from "./constants";
@ -33,6 +34,11 @@ export function activate(context: ExtensionContext) {
window.onDidChangeActiveTextEditor(_handleChangeActiveTextEditor),
window.onDidChangeTextEditorSelection(_handleChangeTextEditorSelection)
);
/* Tree View Features */
context.subscriptions.push(
window.registerTreeDataProvider("xmlTreeView", new XmlTreeDataProvider(context))
);
}
export function deactivate() {

View File

@ -14,7 +14,7 @@ export interface XmlFormattingOptions {
export class XmlFormattingOptionsFactory {
static getXmlFormattingOptions(formattingOptions: FormattingOptions, eol: EndOfLine): XmlFormattingOptions {
const config = workspace.getConfiguration(constants.extensionPrefix);
return {
editorOptions: formattingOptions,
newLine: (eol === EndOfLine.CRLF) ? "\r\n" : "\n",
@ -23,4 +23,4 @@ export class XmlFormattingOptionsFactory {
splitXmlnsOnFormat: config.get<boolean>("splitXmlnsOnFormat")
};
}
}
}

View File

@ -0,0 +1,142 @@
import { commands, window, workspace } from "vscode";
import {
Event, EventEmitter, ExtensionContext, TextEditor, TreeDataProvider,
TreeItem, TreeItemCollapsibleState
} from "vscode";
import * as path from "path";
import { DOMParser } from "xmldom";
import * as constants from "../constants";
export class XmlTreeDataProvider implements TreeDataProvider<any> {
private _onDidChangeTreeData: EventEmitter<any> = new EventEmitter<any>();
private _xmlDocument: any;
constructor(private _context: ExtensionContext) {
window.onDidChangeActiveTextEditor(() => {
this._refreshTree();
});
workspace.onDidChangeTextDocument(() => {
this._refreshTree();
});
}
onDidChangeTreeData = this._onDidChangeTreeData.event;
get activeEditor(): TextEditor {
return window.activeTextEditor || null;
}
getTreeItem(element: any): TreeItem | Thenable<TreeItem> {
const treeItem = new TreeItem(element.localName);
if (this._getChildAttributeArray(element).length > 0) {
treeItem.collapsibleState = TreeItemCollapsibleState.Collapsed;
}
if (this._getChildElementArray(element).length > 0) {
treeItem.collapsibleState = TreeItemCollapsibleState.Collapsed;
}
treeItem.command = {
command: "revealLine",
title: "",
arguments: [{
lineNumber: element.lineNumber - 1,
at: "top"
}]
};
treeItem.iconPath = this._getIcon(element);
return treeItem;
}
getChildren(element?: any): any[] | Thenable<any[]> {
if (!this._xmlDocument) {
this._refreshTree();
}
if (element) {
return [].concat(this._getChildAttributeArray(element), this._getChildElementArray(element));
}
else if (this._xmlDocument) {
return [this._xmlDocument.lastChild];
}
else {
return [];
}
}
private _getChildAttributeArray(node: any): any[] {
if (!node.attributes) {
return [];
}
const array = new Array<any>();
for (let i = 0; i < node.attributes.length; i++) {
array.push(node.attributes[i]);
}
return array;
}
private _getChildElementArray(node: any): any[] {
if (!node.childNodes) {
return [];
}
const array = new Array<any>();
for (let i = 0; i < node.childNodes.length; i++) {
let child = node.childNodes[i];
if (child.tagName) {
array.push(child);
}
}
return array;
}
private _getIcon(element: any): any {
let type = "element";
if (!element.tagName) {
type = "attribute";
}
const icon = {
dark: this._context.asAbsolutePath(path.join("resources", "icons", `${type}.dark.svg`)),
light: this._context.asAbsolutePath(path.join("resources", "icons", `${type}.light.svg`))
};
return icon;
}
private _refreshTree(): void {
if (!this.activeEditor || this.activeEditor.document.languageId !== "xml") {
commands.executeCommand(constants.commands.setContext, constants.contextKeys.xmlTreeViewEnabled, false);
this._xmlDocument = null;
this._onDidChangeTreeData.fire();
return;
}
const config = workspace.getConfiguration(constants.extensionPrefix);
const enableTreeView = config.get<boolean>(constants.configKeys.enableXmlTreeView, true);
commands.executeCommand(constants.commands.setContext, constants.contextKeys.xmlTreeViewEnabled, enableTreeView);
const xml = this.activeEditor.document.getText();
this._xmlDocument = new DOMParser().parseFromString(xml, "text/xml");
this._onDidChangeTreeData.fire();
}
}

View File

@ -4,6 +4,7 @@
"target": "es6",
"outDir": "out",
"lib": [
"dom",
"es6"
],
"sourceMap": true,