mirror of
https://github.com/rabirabirara/obsidian-jelly-snippets.git
synced 2026-07-22 07:30:22 +00:00
Compare commits
6 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
18204f227c | ||
|
|
8a88ae3a68 | ||
|
|
e05c88e941 | ||
|
|
7313ef9d44 | ||
|
|
2e1764a3bf | ||
|
|
4a50f993b7 |
5 changed files with 38 additions and 38 deletions
|
|
@ -115,8 +115,10 @@ It's since been incorporated back into [julia.vim](https://github.com/JuliaEdito
|
|||
- [ ] Regex capabilities - at least on selection.
|
||||
- [ ] A backup for snippets - or a way of specifying snippets in a file, to modularize snippets. May be useful if regex snippets need to be defined separately from text snippets.
|
||||
- [ ] Bugfixes and auditing.
|
||||
- [ ] AUTOMATED TESTING. Use Jest.
|
||||
- [ ] Do the things liamcain suggested in my plugin PR to the Obsidian plugin repo. (one half done)
|
||||
- [ ] Working with Obsidians template stuff.
|
||||
- [ ] REWRITE: CodeMirror editor extension. [See here on state fields.](https://docs.obsidian.md/Plugins/Editor/State+fields) This should also make things such as tabstops and working in live preview possible, but I don't know for sure. This would mark a version upgrade.
|
||||
|
||||
### Other caveats
|
||||
|
||||
|
|
|
|||
63
main.ts
63
main.ts
|
|
@ -8,8 +8,10 @@ import {
|
|||
Setting,
|
||||
} from "obsidian";
|
||||
|
||||
import { Symbol } from "symbol";
|
||||
import { LHS, RHS, Snippet } from "snippet";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
|
||||
import { Symbol } from "src/symbol";
|
||||
import { LHS, RHS, Snippet, SnippetType } from "src/snippet";
|
||||
|
||||
enum AutoTriggerOptions {
|
||||
Disabled = "disabled",
|
||||
|
|
@ -17,13 +19,6 @@ enum AutoTriggerOptions {
|
|||
EnabledYesWS = "y-ws",
|
||||
}
|
||||
|
||||
enum SnippetType {
|
||||
SLSR = 0,
|
||||
SLMR = 1,
|
||||
MLSR = 2,
|
||||
MLMR = 3,
|
||||
}
|
||||
|
||||
interface JellySnippetsSettings {
|
||||
snippetsFile: string;
|
||||
triggerOnSpace: AutoTriggerOptions;
|
||||
|
|
@ -279,40 +274,36 @@ export default class JellySnippets extends Plugin {
|
|||
pos?: EditorPosition
|
||||
): Snippet | undefined {
|
||||
const curpos = pos ? pos : editor.getCursor();
|
||||
const curoffset = editor.posToOffset(curpos);
|
||||
|
||||
// @ts-expect-error
|
||||
const view = editor.cm as EditorView;
|
||||
|
||||
for (let [lhs, rhs] of Object.entries(this.multilineSnippets)) {
|
||||
if (!this.selectBackN(editor, lhs.length, curpos)) {
|
||||
// console.log(
|
||||
// "Error: failed to select back N at: " +
|
||||
// pos +
|
||||
// " with lhs: " +
|
||||
// lhs
|
||||
// );
|
||||
continue;
|
||||
}
|
||||
const from = curoffset - lhs.length;
|
||||
const to = curoffset;
|
||||
|
||||
// Get the text just before the cursor
|
||||
let selected = view.state.sliceDoc(from, to);
|
||||
|
||||
// If the selected string is the LHS, replace it!
|
||||
let selected = editor.getSelection();
|
||||
if (lhs === selected) {
|
||||
editor.replaceSelection(rhs.data);
|
||||
// Dispatch the replacement and move the cursor to where it should be.
|
||||
view.dispatch({
|
||||
changes: [
|
||||
{
|
||||
from,
|
||||
to,
|
||||
insert: rhs.data,
|
||||
},
|
||||
],
|
||||
selection: {
|
||||
anchor: from + rhs.data.length - rhs.info.cursorEnd,
|
||||
},
|
||||
});
|
||||
|
||||
// Reset selection to where the cursor is *after* replacement.
|
||||
// Allows "enabled-with-whitespace" auto replacements to work.
|
||||
this.unselect(editor);
|
||||
|
||||
// Now move cursor back until it has reached end.
|
||||
// * This may be obvious, but since I'm uncertain if setCursor can take an offset, I'm making an extra translation back to EditorPosition
|
||||
editor.setCursor(
|
||||
editor.offsetToPos(
|
||||
editor.posToOffset(editor.getCursor()) -
|
||||
rhs.info.cursorEnd
|
||||
)
|
||||
);
|
||||
// Return what you replaced.
|
||||
return { lhs, rhs };
|
||||
}
|
||||
|
||||
// Reset selection to where the cursor is.
|
||||
this.unselect(editor, curpos);
|
||||
}
|
||||
|
||||
// No replace - return undefined
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"id": "jelly-snippets",
|
||||
"name": "Jelly Snippets",
|
||||
"version": "0.1.8",
|
||||
"version": "0.2.0",
|
||||
"minAppVersion": "0.15.0",
|
||||
"description": "A simple plugin for text snippets, with auto replacement",
|
||||
"author": "Spencer Gouw",
|
||||
|
|
|
|||
|
|
@ -11,3 +11,10 @@ export type Snippet = {
|
|||
lhs: LHS;
|
||||
rhs: RHS;
|
||||
};
|
||||
|
||||
export enum SnippetType {
|
||||
SLSR = 0,
|
||||
SLMR = 1,
|
||||
MLSR = 2,
|
||||
MLMR = 3,
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { RHS } from "snippet";
|
||||
import { RHS } from "src/snippet";
|
||||
|
||||
export enum Symbol {
|
||||
// on parse
|
||||
Loading…
Reference in a new issue