-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextension.js
More file actions
139 lines (117 loc) · 4.42 KB
/
Copy pathextension.js
File metadata and controls
139 lines (117 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const vscode = require('vscode');
let statusBarItem;
function activate(context) {
statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
statusBarItem.command = 'yml-json-key-finder.copyPath';
statusBarItem.tooltip = 'Click to copy path'
context.subscriptions.push(statusBarItem);
const cursorPositionDisposable = vscode.window.onDidChangeTextEditorSelection(event => {
if (event.textEditor === vscode.window.activeTextEditor) {
showFullPathInStatusBar(event.textEditor);
}
});
let findKeyCommand = vscode.commands.registerCommand('yml-json-key-finder.searchKey', () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return; // No open text editor
}
vscode.window.showInputBox({ prompt: 'Enter Key Path' }).then((keyPath) => {
if (keyPath) {
const position = findKeyPosition(editor, keyPath);
if (position) {
const lineRange = editor.document.lineAt(position.line).range;
editor.selection = new vscode.Selection(lineRange.start, lineRange.end);
editor.revealRange(lineRange);
} else {
vscode.window.showErrorMessage('Key not found');
}
}
});
});
let copyPathCommand = vscode.commands.registerCommand('yml-json-key-finder.copyPath', () => {
vscode.env.clipboard.writeText(statusBarItem.text.replace('Full Path: ', ''));
vscode.window.showInformationMessage('Path copied to clipboard');
});
context.subscriptions.push(cursorPositionDisposable, findKeyCommand, copyPathCommand);
}
function findKeyPosition(editor, path) {
const document = editor.document;
const keys = path.split('.');
let regexPatterns = keys.map((key, index) => {
let regexString;
if (document.languageId === 'json') {
regexString = `\\"${key}\\"\\s*:\\s*`;
} else {
regexString = index === keys.length - 1 ? `\\b${key}\\b\\s*:\\s*` : `\\b${key}\\b\\s*:`;
}
return new RegExp(regexString);
});
let currentLevel = 0;
for (let i = 0; i < document.lineCount; i++) {
const lineText = document.lineAt(i).text;
if (currentLevel < keys.length - 1) {
if (regexPatterns[currentLevel].test(lineText)) {
currentLevel++;
}
} else {
const finalKeyRegex = regexPatterns[currentLevel];
const match = finalKeyRegex.exec(lineText);
if (match) {
return new vscode.Position(i, match.index);
}
}
}
return null;
}
function showFullPathInStatusBar(editor) {
if (!editor || !editor.document) {
return;
}
const position = editor.selection.active;
const lineText = editor.document.lineAt(position.line).text;
const fullPath = calculateFullPath(lineText, position, editor.document);
if (fullPath) {
statusBarItem.text = `Full Path: ${fullPath}`;
statusBarItem.show();
} else {
statusBarItem.hide();
}
}
function calculateFullPath(lineText, position, document) {
let currentLevel = position.line;
let path = [];
let currentIndentation = getIndentation(document.lineAt(currentLevel).text);
// Include the key at the current line
let currentLineKey = getKeyFromLine(document.lineAt(currentLevel).text);
if (currentLineKey) {
path.push(currentLineKey);
}
while (currentLevel >= 0) {
const line = document.lineAt(currentLevel).text;
const lineIndentation = getIndentation(line);
if (lineIndentation < currentIndentation) {
currentIndentation = lineIndentation;
let keyMatch = getKeyFromLine(line);
if (keyMatch) {
path.unshift(keyMatch);
}
}
currentLevel--;
}
return path.join('.');
}
// Helper function to determine the indentation level of a line
function getIndentation(lineText) {
const matches = lineText.match(/^(\s*)/);
return matches ? matches[1].length : 0;
}
// Helper function to extract a key from a line
function getKeyFromLine(lineText) {
let keyMatch = lineText.match(/"([^"]+)"\s*:/) || lineText.match(/([\w-]+)\s*:/);
return keyMatch ? keyMatch[1] : null;
}
function deactivate() {}
module.exports = {
activate,
deactivate
};