forked from aubinlrx/vscode-yamlnav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
61 lines (48 loc) · 1.92 KB
/
extension.js
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
const vscode = require('vscode')
const path = require('path')
const fs = require('fs')
const { list, copy } = require('./src/yaml-path')
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let searchPaths = vscode.commands.registerTextEditorCommand('yamlnav.search', function (editor) {
const file = editor.document.fileName
if (path.extname(file) !== '.yml' && path.extname(file) !== '.yaml') {
vscode.window.showErrorMessage("Only works with .yml or .yaml files.")
return
}
const content = fs.readFileSync(file, 'utf8')
const results = list(content)
vscode.window.showQuickPick(results.map(item => item.path))
.then(selected => {
if (!selected) return
const lineNumber = results.find(item => item.path === selected).line.line - 1
const line = editor.document.lineAt(lineNumber)
const characterIndex = line.firstNonWhitespaceCharacterIndex
const position = new vscode.Position(lineNumber, characterIndex)
editor.selections = [new vscode.Selection(position,position)];
const range = new vscode.Range(position, position);
editor.revealRange(range);
})
});
let copyPath = vscode.commands.registerTextEditorCommand('yamlnav.copy', function(editor) {
const file = editor.document.fileName
if (path.extname(file) !== '.yml' && path.extname(file) !== '.yaml') {
vscode.window.showErrorMessage("Only works with .yml or .yaml files.")
return
}
const lineNumber = editor.selection.active.line + 1
const content = fs.readFileSync(file, 'utf8')
const nodePath = copy(content, lineNumber)
vscode.env.clipboard.writeText(nodePath.path)
vscode.window.showInformationMessage(nodePath.path)
});
context.subscriptions.push(searchPaths, copyPath);
}
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
}