Skip to content

Commit

Permalink
add setting onEmptyLineAction
Browse files Browse the repository at this point in the history
add setting setCursorOnNewLine
git enabled in workspace settings
smartstep enabled in launch.json
update CHANGELOG
  • Loading branch information
midnightsyntax committed Nov 7, 2017
1 parent 05f1ff9 commit 63a541e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 12 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"smartStep": true,
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/out"],
"preLaunchTask": "npm"
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"search.exclude": {
"out": true // set this to false to include "out" folder in search results
},
"typescript.tsdk": "./node_modules/typescript/lib" // we want to use the TS server from our node_modules folder to control its version
"typescript.tsdk": "./node_modules/typescript/lib",
"git.enabled": true // we want to use the TS server from our node_modules folder to control its version
}
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

# CHANGELOG

## 1.4.0
##### 2017-11-07

### Changed

- [Down] and [Up] will now create a new line by default. If the target line is empty you can now decide if you rather have 'console.log' replace the empty line. Set this behaviour with the new `.onEmptyLineAction` setting.

### Added

- New config setting `.onEmptyLineAction`
- New config setting `.setCursorOnNewLine`

By setting `.setCursorOnNewLine` to `true` the cursor will move to the new line when one is created by wrapping 'console.log' [Down] or [Up].


---

## 1.3.3
##### 2017-09-11

Expand All @@ -13,7 +30,6 @@

---


## 1.3.2
##### 2017-09-10

Expand Down
22 changes: 19 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "git",
"url": "https://github.com/midnightsyntax/vscode-wrap-console-log"
},
"version": "1.3.3",
"version": "1.4.0",
"publisher": "midnightsyntax",
"icon": "images/icon.png",
"engines": {
Expand Down Expand Up @@ -37,16 +37,32 @@
"title": "Wrap Console Log",
"type": "object",
"properties": {
"wrap-console-log.setCursorOnNewLine": {
"type": "boolean",
"title": "Make the cursor jump to the same line as 'console.log'",
"description": "On true the cursor will move to where 'console.log' got inserted. If false the cursor will stay on the same spot as when you executed the command.",
"default": false
},
"wrap-console-log.onEmptyLineAction": {
"type": "string",
"enum": [
"Insert",
"Replace"
],
"title": "Set the default action when wrapping to empty lines with [Down] and [Up].",
"description": "On \"Insert\" a new line will be created. On \"Replace\" the empty line will be replaced.",
"default": "Insert"
},
"wrap-console-log.alwaysInputBoxOnPrefix": {
"type": "boolean",
"title": "Always show input box when wrapping with a prefix",
"title": "Always show input box when wrapping with a prefix.",
"description": "If true an input box will always show when logging with a prefix.",
"default": false
},
"wrap-console-log.alwaysUsePrefix": {
"type": "boolean",
"title": "Always log with the wrapped word as prefix.",
"description": "If this is set to 'true' even the \"non-prefix-commands\" will log with prefix.",
"description": "If true ALL wraps will log with the selected word as prefix.",
"default": false
},
"wrap-console-log.autoFormat": {
Expand Down
57 changes: 50 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,33 @@ function handle(target: Wrap, prefix?: boolean, input?: boolean) {

}).then((wrap:WrapData) => {

let onEmptyAction = getSetting("onEmptyLineAction");
let setCursor = getSetting("setCursorOnNewLine");

function SetCursor(l) {
let curp = new vscode.Position(l, currentEditor.document.lineAt(l).range.end.character);
currentEditor.selection = new vscode.Selection(curp, curp)
}

function getTargetLine(go: Wrap) {
let stop = false;
let li = wrap.line;
let l = 0;
while (!stop) {
if (go == Wrap.Down) {
li++;
} else { li-- }
if (li < wrap.doc.lineCount) {
if (!wrap.doc.lineAt(li).isEmptyOrWhitespace) {
l = li; stop = true;
}
} else {
stop = true;
}
}
return li;
}

switch (target) {

case Wrap.Inline: {
Expand All @@ -78,20 +105,31 @@ function handle(target: Wrap, prefix?: boolean, input?: boolean) {
} break;

case Wrap.Up: {

let tLine = wrap.doc.lineAt(wrap.line-1);
let tLineEmpty = tLine.text.trim() == '' ? true : false;
let tLineInd = tLine.text.substring(0, tLine.firstNonWhitespaceCharacterIndex);

currentEditor.edit(function(e) {
e.insert(new vscode.Position(wrap.line, wrap.idx), wrap.txt.concat('\n', wrap.ind));
if (tLineEmpty && onEmptyAction == "Replace") {
e.replace(new vscode.Position(tLine.lineNumber, 0), wrap.ind.concat(wrap.txt));
} else {
e.insert(new vscode.Position(wrap.line, wrap.idx), wrap.txt.concat('\n', wrap.ind));
}
}).then(() => {
if (setCursor) SetCursor(wrap.line);
});
} break;

case Wrap.Down: {

let nxtLine: vscode.TextLine;
let nxtLineEmpty: boolean;
let nxtLineInd: string;
let nxtNonEmpty: vscode.TextLine

if (!wrap.lastLine) {
nxtLine = wrap.doc.lineAt(wrap.line+1);
nxtLineEmpty = nxtLine.text.trim() == '' ? true : false;
nxtNonEmpty = (nxtLine.isEmptyOrWhitespace) ? wrap.doc.lineAt(getTargetLine(Wrap.Down)) : undefined;
nxtLineInd = nxtLine.text.substring(0, nxtLine.firstNonWhitespaceCharacterIndex);
} else {
nxtLineInd = "";
Expand All @@ -101,14 +139,18 @@ function handle(target: Wrap, prefix?: boolean, input?: boolean) {
let pos = new vscode.Position(wrap.line, wrap.doc.lineAt(wrap.line).range.end.character);

currentEditor.edit(function(e) {
currentEditor.edit(function(e) {
if (nxtLineEmpty) {
e.replace(new vscode.Position(nxtLine.lineNumber, 0), wrap.ind.concat(wrap.txt));
if (nxtLine != undefined) {
if (nxtLine.isEmptyOrWhitespace) {
if (onEmptyAction == "Insert") {
e.insert(new vscode.Position(wrap.line, wrap.doc.lineAt(wrap.line).range.end.character), "\n".concat((nxtLineInd > wrap.ind ? nxtLineInd : wrap.ind), wrap.txt));
} else if (onEmptyAction == "Replace") {
e.replace(new vscode.Position(nxtLine.lineNumber, 0), wrap.ind.concat(wrap.txt));
}
} else {
e.insert(new vscode.Position(wrap.line, wrap.doc.lineAt(wrap.line).range.end.character),
"\n".concat((nxtLineInd.length > wrap.ind.length ? nxtLineInd : wrap.ind), wrap.txt));
}
})
}
}).then(() => {
if (vscode.workspace.getConfiguration("wrap-console-log")["autoFormat"] == true && !wrap.lastLine) {
let nextLineEnd = wrap.doc.lineAt(wrap.line+2).range.end;
Expand All @@ -122,6 +164,7 @@ function handle(target: Wrap, prefix?: boolean, input?: boolean) {
} else {
currentEditor.selection = wrap.sel;
}
if (setCursor) SetCursor(nxtLine.lineNumber);
})
}

Expand Down

0 comments on commit 63a541e

Please sign in to comment.