Improve Typing
This commit is contained in:
parent
6b059c192e
commit
c0d1bb5486
@ -29,14 +29,14 @@ export class XmlTreeDataProvider implements TreeDataProvider<any> {
|
|||||||
return window.activeTextEditor || null;
|
return window.activeTextEditor || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
getTreeItem(element: any): TreeItem | Thenable<TreeItem> {
|
getTreeItem(element: Node): TreeItem | Thenable<TreeItem> {
|
||||||
const treeItem = new TreeItem(element.localName);
|
const treeItem = new TreeItem(element.localName);
|
||||||
|
|
||||||
if (this._getChildAttributeArray(element).length > 0) {
|
if (this._isElement(element) && this._getChildAttributeArray(<Element>element).length > 0) {
|
||||||
treeItem.collapsibleState = TreeItemCollapsibleState.Collapsed;
|
treeItem.collapsibleState = TreeItemCollapsibleState.Collapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._getChildElementArray(element).length > 0) {
|
if (this._isElement(element) && this._getChildElementArray(<Element>element).length > 0) {
|
||||||
treeItem.collapsibleState = TreeItemCollapsibleState.Collapsed;
|
treeItem.collapsibleState = TreeItemCollapsibleState.Collapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ export class XmlTreeDataProvider implements TreeDataProvider<any> {
|
|||||||
command: constants.nativeCommands.revealLine,
|
command: constants.nativeCommands.revealLine,
|
||||||
title: "",
|
title: "",
|
||||||
arguments: [{
|
arguments: [{
|
||||||
lineNumber: element.lineNumber - 1,
|
lineNumber: (element as any).lineNumber - 1,
|
||||||
at: "top"
|
at: "top"
|
||||||
}]
|
}]
|
||||||
};
|
};
|
||||||
@ -54,13 +54,13 @@ export class XmlTreeDataProvider implements TreeDataProvider<any> {
|
|||||||
return treeItem;
|
return treeItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
getChildren(element?: any): any[] | Thenable<any[]> {
|
getChildren(element?: Node): any[] | Thenable<any[]> {
|
||||||
if (!this._xmlDocument) {
|
if (!this._xmlDocument) {
|
||||||
this._refreshTree();
|
this._refreshTree();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (element) {
|
if (this._isElement(element)) {
|
||||||
return [].concat(this._getChildAttributeArray(element), this._getChildElementArray(element));
|
return [].concat(this._getChildAttributeArray(<Element>element), this._getChildElementArray(<Element>element));
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (this._xmlDocument) {
|
else if (this._xmlDocument) {
|
||||||
@ -72,7 +72,7 @@ export class XmlTreeDataProvider implements TreeDataProvider<any> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private _getChildAttributeArray(node: any): any[] {
|
private _getChildAttributeArray(node: Element): any[] {
|
||||||
if (!node.attributes) {
|
if (!node.attributes) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@ -86,7 +86,7 @@ export class XmlTreeDataProvider implements TreeDataProvider<any> {
|
|||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _getChildElementArray(node: any): any[] {
|
private _getChildElementArray(node: Element): any[] {
|
||||||
if (!node.childNodes) {
|
if (!node.childNodes) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@ -96,7 +96,7 @@ export class XmlTreeDataProvider implements TreeDataProvider<any> {
|
|||||||
for (let i = 0; i < node.childNodes.length; i++) {
|
for (let i = 0; i < node.childNodes.length; i++) {
|
||||||
const child = node.childNodes[i];
|
const child = node.childNodes[i];
|
||||||
|
|
||||||
if (child.tagName) {
|
if (this._isElement(child)) {
|
||||||
array.push(child);
|
array.push(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,10 +104,10 @@ export class XmlTreeDataProvider implements TreeDataProvider<any> {
|
|||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _getIcon(element: any): any {
|
private _getIcon(element: Node): any {
|
||||||
let type = "element";
|
let type = "element";
|
||||||
|
|
||||||
if (!element.tagName) {
|
if (!this._isElement(element)) {
|
||||||
type = "attribute";
|
type = "attribute";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,6 +119,10 @@ export class XmlTreeDataProvider implements TreeDataProvider<any> {
|
|||||||
return icon;
|
return icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _isElement(node: Node): boolean {
|
||||||
|
return (!!node && !!(node as Element).tagName);
|
||||||
|
}
|
||||||
|
|
||||||
private _refreshTree(): void {
|
private _refreshTree(): void {
|
||||||
if (!this.activeEditor || this.activeEditor.document.languageId !== constants.languageIds.xml) {
|
if (!this.activeEditor || this.activeEditor.document.languageId !== constants.languageIds.xml) {
|
||||||
commands.executeCommand(constants.nativeCommands.setContext, constants.contextKeys.xmlTreeViewEnabled, false);
|
commands.executeCommand(constants.nativeCommands.setContext, constants.contextKeys.xmlTreeViewEnabled, false);
|
||||||
@ -134,9 +138,15 @@ export class XmlTreeDataProvider implements TreeDataProvider<any> {
|
|||||||
commands.executeCommand(constants.nativeCommands.setContext, constants.contextKeys.xmlTreeViewEnabled, enableTreeView);
|
commands.executeCommand(constants.nativeCommands.setContext, constants.contextKeys.xmlTreeViewEnabled, enableTreeView);
|
||||||
|
|
||||||
const xml = this.activeEditor.document.getText();
|
const xml = this.activeEditor.document.getText();
|
||||||
this._xmlDocument = new DOMParser().parseFromString(xml, "text/xml");
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
this._xmlDocument = new DOMParser().parseFromString(xml, "text/xml");
|
||||||
this._onDidChangeTreeData.fire();
|
this._onDidChangeTreeData.fire();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
catch {
|
||||||
|
this._xmlDocument = new DOMParser().parseFromString("<InvalidDocument />", "text/xml");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user