generated from see7e/github-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
69 lines (56 loc) · 2.1 KB
/
extension.js
File metadata and controls
69 lines (56 loc) · 2.1 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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.window.onDidChangeActiveTextEditor(editor => {
if (editor && editor.document.languageId === 'python') {
foldDocstrings(editor);
}
});
context.subscriptions.push(disposable);
}
/**
* Folds all multiline docstrings in the given editor.
*
* @param {vscode.TextEditor} editor - The editor instance containing the document to process.
*/
function foldDocstrings(editor) {
const document = editor.document;
setTimeout(() => {
const text = document.getText();
// console.log("Document Text:", text);
if (!text) {
console.log("⚠️ Warning: getText() returned an empty string.");
return;
}
const regex = /"""\s*([\s\S]*?)\s*"""/g; // Match multiline docstrings
let ranges = [];
let match;
while ((match = regex.exec(text)) !== null) {
let startLine = document.positionAt(match.index).line + 1;
let endLine = document.positionAt(match.index + match[0].length).line + 1;
if (startLine < endLine) {
console.log(`Docstring found from line ${startLine} to ${endLine}`);
ranges.push(new vscode.Range(startLine, 0, endLine, 0));
}
}
if (ranges.length === 0) {
console.log("✅ No docstrings found.");
return;
}
// Apply folding using selections and command
editor.selections = ranges.map(range => new vscode.Selection(range.start, range.start));
vscode.commands.executeCommand("editor.fold");
}, 250);
}
// This method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
}