Add Get Current XPath
This commit also moves some shared code to a common class. Issue: #85
This commit is contained in:
parent
af2060dc38
commit
6a92fa96f2
10 changed files with 223 additions and 84 deletions
21
src/xpath/commands/getCurrentXPath.ts
Normal file
21
src/xpath/commands/getCurrentXPath.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import { window } from "vscode";
|
||||
import { TextEditor, TextEditorEdit } from "vscode";
|
||||
import { DOMParser } from "xmldom";
|
||||
|
||||
import { XPathBuilder } from "../xpath-builder";
|
||||
|
||||
export function getCurrentXPath(editor: TextEditor, edit: TextEditorEdit): void {
|
||||
if (!editor.selection) {
|
||||
window.showInformationMessage("Please put your cursor in an element or attribute name.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const document = new DOMParser().parseFromString(editor.document.getText());
|
||||
const xpath = new XPathBuilder(document).build(editor.selection.start);
|
||||
|
||||
window.showInputBox({
|
||||
value: xpath,
|
||||
valueSelection: undefined
|
||||
});
|
||||
}
|
||||
|
|
@ -1 +1,2 @@
|
|||
export * from "./evaluateXPath";
|
||||
export * from "./getCurrentXPath";
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
export * from "./xpath-builder";
|
||||
export * from "./xpath-evaluator";
|
||||
|
|
|
|||
41
src/xpath/xpath-builder.ts
Normal file
41
src/xpath/xpath-builder.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import { Position } from "vscode";
|
||||
import { DOMParser } from "xmldom";
|
||||
|
||||
import { XmlTraverser } from "../common";
|
||||
|
||||
export class XPathBuilder {
|
||||
|
||||
private _xmlTraverser: XmlTraverser;
|
||||
|
||||
constructor(private _xmlDocument: Document) {
|
||||
this._xmlTraverser = new XmlTraverser(this._xmlDocument);
|
||||
}
|
||||
|
||||
build(position: Position): string {
|
||||
const selectedNode = this._xmlTraverser.getNodeAtPosition(position);
|
||||
|
||||
return this._buildCore(selectedNode);
|
||||
}
|
||||
|
||||
private _buildCore(selectedNode: Node): string {
|
||||
if (selectedNode === this._xmlDocument.documentElement) {
|
||||
return `/${selectedNode.nodeName}`;
|
||||
}
|
||||
|
||||
if (!this._xmlTraverser.isElement(selectedNode)) {
|
||||
return `${this._buildCore((selectedNode as any).ownerElement)}/@${selectedNode.nodeName}`;
|
||||
}
|
||||
|
||||
else if (this._xmlTraverser.hasSimilarSiblings(selectedNode)) {
|
||||
const siblings = this._xmlTraverser.getSiblings(selectedNode);
|
||||
const xPathIndex = (siblings.indexOf(selectedNode) + 1);
|
||||
|
||||
return `${this._buildCore(selectedNode.parentNode)}/${selectedNode.nodeName}[${xPathIndex}]`;
|
||||
}
|
||||
|
||||
else {
|
||||
return `${this._buildCore(selectedNode.parentNode)}/${selectedNode.nodeName}`;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue