-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencodingEol.js
More file actions
81 lines (70 loc) · 3.12 KB
/
Copy pathencodingEol.js
File metadata and controls
81 lines (70 loc) · 3.12 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
/*---------------------------------------------------------------------------------------------
* LevelCode — Notepad++ Pack
* Feature: Encoding & line-ending controls (M1, feature 3).
*
* VS Code already shows encoding/EOL in the status bar, but converting line endings there
* is a multi-step picker. We add:
* - a one-click EOL toggle in the status bar (LF ⇄ CRLF),
* - direct one-step commands (Convert to LF / CRLF / Toggle),
* - a shortcut to the built-in encoding picker (reopen / save with encoding).
*--------------------------------------------------------------------------------------------*/
// @ts-check
'use strict';
const vscode = require('vscode');
/** @type {vscode.StatusBarItem} */
let eolStatus;
function isCRLF(doc) {
return doc.eol === vscode.EndOfLine.CRLF;
}
function refreshEol() {
const ed = vscode.window.activeTextEditor;
if (!ed) { eolStatus.hide(); return; }
eolStatus.text = isCRLF(ed.document) ? '$(arrow-swap) CRLF · Windows' : '$(arrow-swap) LF · macOS/Unix';
eolStatus.tooltip = isCRLF(ed.document)
? 'LevelCode: Windows line endings (CRLF). Click to switch to macOS/Unix (LF).'
: 'LevelCode: macOS/Unix line endings (LF). Click to switch to Windows (CRLF).';
eolStatus.command = 'levelcode.eol.toggle';
eolStatus.show();
}
/** @param {vscode.EndOfLine} target */
async function setEol(target) {
const ed = vscode.window.activeTextEditor;
if (!ed) { vscode.window.showWarningMessage('LevelCode: open a text editor first.'); return; }
if (ed.document.eol === target) {
vscode.window.setStatusBarMessage('LevelCode: line endings already set', 1500);
return;
}
await ed.edit((eb) => eb.setEndOfLine(target));
refreshEol();
vscode.window.setStatusBarMessage(
`$(check) LevelCode: converted line endings to ${target === vscode.EndOfLine.CRLF ? 'CRLF' : 'LF'}`, 2000);
}
function toggleEol() {
const ed = vscode.window.activeTextEditor;
if (!ed) { vscode.window.showWarningMessage('LevelCode: open a text editor first.'); return; }
return setEol(isCRLF(ed.document) ? vscode.EndOfLine.LF : vscode.EndOfLine.CRLF);
}
/** @param {vscode.ExtensionContext} context */
function registerEncodingEol(context) {
eolStatus = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 99);
context.subscriptions.push(eolStatus);
context.subscriptions.push(
vscode.commands.registerCommand('levelcode.eol.toLF', () => setEol(vscode.EndOfLine.LF)),
vscode.commands.registerCommand('levelcode.eol.toCRLF', () => setEol(vscode.EndOfLine.CRLF)),
vscode.commands.registerCommand('levelcode.eol.toggle', toggleEol),
// Built-in encoding picker (Reopen with Encoding / Save with Encoding).
vscode.commands.registerCommand('levelcode.encoding.change',
() => vscode.commands.executeCommand('workbench.action.editor.changeEncoding'))
);
// Keep the status item in sync.
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(refreshEol),
vscode.workspace.onDidChangeTextDocument((e) => {
if (vscode.window.activeTextEditor && e.document === vscode.window.activeTextEditor.document) {
refreshEol();
}
})
);
refreshEol();
}
module.exports = { registerEncodingEol };