Skip to content

Commit

Permalink
Added commands: sort selected lines and split selections on new line;…
Browse files Browse the repository at this point in the history
… Updated README.md, change manifest to new version 1.1.0, added new gif files
  • Loading branch information
KrazyManJ committed Dec 20, 2022
1 parent 306ab4a commit 8e8e917
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 26 deletions.
52 changes: 47 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<h1 align=center>Keyshots</h1>

Adds classic hotkey/shortcuts commands from popular IDEs like Visual Studio Code or JetBrains Family. Adds actions like...
<p align=center><i>Turn your Obsidian into compact text editor!</i></p>

---

Keyshots is an [Obsidian](https://obsidian.md) plugin that adds classic hotkey/shortcuts commands from popular IDEs like Visual Studio Code or JetBrains Family.

Apart from that, i want to add as much my custom and usefull commands as possible!

Adds actions like...

...move line up or down (<kbd>Shift + Alt + ↑</kbd> / <kbd>Shift + Alt + ↓</kbd>) ...

![](assets/line_move.gif)

...add caret cursors up or down (<kbd>Ctrl + Alt + ↑</kbd> / <kbd>Ctrl + Alt + ↓</kbd>) ...
...add caret cursor up or down (<kbd>Ctrl + Alt + ↑</kbd> / <kbd>Ctrl + Alt + ↓</kbd>) ...

![](assets/add_caret.gif)

Expand All @@ -18,8 +26,42 @@ Adds classic hotkey/shortcuts commands from popular IDEs like Visual Studio Code

![](assets/duplicate_text.gif)

...change readable line length inside editor (<kbd>Ctrl + Shift + R</kbd>) ...
...toggle readable line length inside editor (<kbd>Ctrl + Shift + R</kbd>) ...

![](assets/toggle_readable_line_length.gif)

...toggle line numbers inside editor (<kbd>Ctrl + Shift + L</kbd>) ...

![](assets/toggle_line_numbers.gif)

...encode or decode URI text (<kbd>Ctrl + Alt + U</kbd>) ...

![](assets/uri_encode_decode.gif)

...transform selected texts to Lowercase (<kbd>Alt + L</kbd>) / Uppercase (<kbd>Alt + U</kbd>) / Titlecase (<kbd>Alt + C</kbd>) ...

![](assets/transform_text.gif)

...join selected lines to one line (<kbd>Crtl + Shift + J</kbd>) ...

![](assets/join_lines.gif)

...split selections on new line (<kbd>Alt + S</kbd>) and trim selection (<kbd>Alt + T</kbd>) ...

![](assets/split_sel_on_line_and_trim.gif)

...sort selected lines with alphanumeric comparison (<kbd>Ctrl + Shift + S</kbd>) ...

![](assets/sort_selected_lines.gif)

...transform selections to or from snakecase (<kbd>Alt + -</kbd>) ...

![](assets/transform_to_from_snakecase.gif)

...**And more commands comming soon**!

## ⚠️ Read before usage:

![](assets/change_readable_line_length.gif)
- `Duplicate line or selection` command is defaultly on the same hotkey as `delete line` command (<kbd>Ctrl + D</kbd>). That is because JetBrains IDEs have it the same way. Before usage change this action hotkey based on your preference or basically clear `Delete line` command!

...**And much more**!
- For most of these commands, it requires more than one undo/revert actions, because these commands contain more than one instruction.
Binary file modified assets/add_caret.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/change_readable_line_length.gif
Binary file not shown.
Binary file modified assets/duplicate_text.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/insert_line.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/join_lines.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/line_move.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sort_selected_lines.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/split_sel_on_line_and_trim.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/toggle_line_numbers.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/toggle_readable_line_length.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/transform_text.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/transform_to_from_snakecase.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/uri_encode_decode.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 70 additions & 20 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,44 @@ function convertURI(editor: Editor) {

const titleCase = (s:string) => s.replace(/\w\S*/g,(txt) => txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase())

function splitSelectedTextOnNewLine(editor: Editor) {
const newSelections: EditorSelectionOrCaret[] = []
let index = 0
editor.listSelections().sort((a,b) => a.anchor.line - b.anchor.line).forEach(sel => {
if (isCaret(sel)) newSelections.push(sel)
else {
const [from, to] = fromToPos(
{ch:sel.anchor.ch,line:sel.anchor.line+index},
{ch:sel.head.ch,line:sel.head.line+index}
)
const tx = editor.getRange(from, to)
editor.replaceRange("\n" + tx + "\n", from, to)
newSelections.push({
anchor: { ch: 0, line:from.line+1 },
head: { ch: editor.getLine(to.line+1).length, line:to.line+1 }
})
index += (tx.match(/\n/g) || []).length+1
}
})
editor.setSelections(newSelections)
}

function sortSelectedLines(editor: Editor) {
const newSelections: EditorSelectionOrCaret[] = []
editor.listSelections().filter(f => !isCaret(f)).forEach(sel => {
const [from, to] = expandLines(editor, ...fromToPos(sel.anchor, sel.head))
console.log(editor.getRange(from, to).split("\n"))
editor.replaceRange(
editor.getRange(from, to).split("\n")
.sort((a, b) => a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }))
.join("\n")
, from, to)
const [nFrom,nTo] = expandLines(editor,from,to)
newSelections.push({anchor:nFrom,head:nTo})
})
editor.setSelections(newSelections)
}

export default class KeyshotsPlugin extends Plugin {

async onload() {
Expand All @@ -176,8 +214,8 @@ export default class KeyshotsPlugin extends Plugin {
========================================================================
*/
this.addCommand({
id: 'change-readable-length',
name: "Change readable line length",
id: 'toggle-readable-length',
name: "Toggle readable line length",
hotkeys: [{ modifiers: ["Mod", "Shift"], key: "R" }],
callback: () => flipBooleanSetting(this.app,'readableLineLength')
});
Expand Down Expand Up @@ -206,13 +244,13 @@ export default class KeyshotsPlugin extends Plugin {
});
this.addCommand({
id: 'add-carets-up',
name: 'Add caret cursors up',
name: 'Add caret cursor up',
hotkeys: [{ modifiers: ["Alt", "Mod"], key: "ArrowUp" }],
editorCallback: (editor) => addCarets(editor, -1, 0)
});
this.addCommand({
id: 'add-carets-down',
name: 'Add caret cursors down',
name: 'Add caret cursor down',
hotkeys: [{ modifiers: ["Alt", "Mod"], key: "ArrowDown" }],
editorCallback: (editor) => addCarets(editor, 1, editor.lineCount())
});
Expand Down Expand Up @@ -247,51 +285,63 @@ export default class KeyshotsPlugin extends Plugin {
editorCallback: (editor) => insertLine(editor, -1)
})
this.addCommand({
id: 'join-lines',
name: "Join Lines",
id: 'join-selected-lines',
name: "Join selected lines",
hotkeys: [{ modifiers: ["Shift", "Mod"], key: "j" }],
editorCallback: (editor) => replaceSelections(editor,(s)=>s.replace(/\n/g,""))
editorCallback: (editor) => replaceSelections(editor, (s) => s.replace(/\n/g,""))
})
this.addCommand({
id: 'split-selections-on-new-line',
name: "Split selections on new line",
hotkeys: [{ modifiers: ["Alt"], key: "s" }],
editorCallback: (editor) => splitSelectedTextOnNewLine(editor)
})
/*
========================================================================
SELECTION TRANSFORMATIONS
========================================================================
*/
this.addCommand({
id: 'trim-selected-text',
name: "Trim selected text",
id: 'trim-selections',
name: "Trim selections",
hotkeys: [{ modifiers: ["Alt"], key: "T" }],
editorCallback: (editor) => replaceSelections(editor, (s) => s.trim())
});
this.addCommand({
id: 'convert-spaces-to-underscores',
name: "Convert selected text to spaces <=> underscores",
id: 'transform-from-to-snake-case',
name: "Transform selections from / to Snakecase",
hotkeys: [{ modifiers: ["Alt"], key: "-" }],
editorCallback: (editor) => convertSpacesUnderScores(editor)
});
this.addCommand({
id: 'encode-or-decode-uri',
name: "Encode / Decode URI",
name: "Encode / Decode URI selections",
hotkeys: [{ modifiers: ["Mod","Alt"], key: "u" }],
editorCallback: (editor) => convertURI(editor)
})
this.addCommand({
id: 'transform-to-lowercase',
name: "Transform selection to lowercase",
id: 'transform-selections-to-lowercase',
name: "Transform selections to Lowercase",
hotkeys: [{ modifiers: ["Alt"], key: "l" }],
editorCallback: (editor) => replaceSelections(editor,(s)=>s.toLowerCase())
editorCallback: (editor) => replaceSelections(editor, (s) => s.toLowerCase())
})
this.addCommand({
id: 'transform-to-uppercase',
name: "Transform selection to uppercase",
id: 'transform-selections-to-uppercase',
name: "Transform selections to Uppercase",
hotkeys: [{ modifiers: ["Alt"], key: "u" }],
editorCallback: (editor) => replaceSelections(editor,(s)=>s.toUpperCase())
editorCallback: (editor) => replaceSelections(editor, (s) => s.toUpperCase())
})
this.addCommand({
id: 'transform-to-titlecase',
name: "Transform selection to titlecase (Capitalize)",
id: 'transform-selections-to-titlecase',
name: "Transform selections to Titlecase (Capitalize)",
hotkeys: [{ modifiers: ["Alt"], key: "c" }],
editorCallback: (editor) => replaceSelections(editor, (s) => titleCase(s))
})
this.addCommand({
id: 'sort-selected-lines',
name: "Sort selected lines",
hotkeys: [{ modifiers: ["Mod","Shift"], key: "s" }],
editorCallback: (editor) => sortSelectedLines(editor)
})
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "keyshots",
"name": "Keyshots",
"version": "1.0.0",
"version": "1.1.0",
"minAppVersion": "0.15.0",
"description": "Adds classic hotkey/shortcuts commands from popular IDEs like Visual Studio Code or JetBrains Family.",
"author": "KrazyManJ",
Expand Down

0 comments on commit 8e8e917

Please sign in to comment.