Compare commits

...

6 commits
0.1.8 ... main

Author SHA1 Message Date
Spencer Gouw
18204f227c chore: update manifest/versions 2023-10-08 19:26:55 -05:00
Spencer Gouw
8a88ae3a68 refac: move SnippetType into snippet.ts 2023-10-08 19:26:36 -05:00
Spencer Gouw
e05c88e941 refac: put extra .ts files into src/ 2023-10-08 18:55:01 -05:00
Spencer Gouw
7313ef9d44 Merge branch 'main' of https://github.com/rabirabirara/obsidian-jelly-snippets 2023-10-08 18:54:25 -05:00
Spencer Gouw
2e1764a3bf fix: use EditorView (CM) to dispatch changes 2023-10-08 18:54:15 -05:00
Spencer Gouw
4a50f993b7
Update README.md 2023-10-08 13:08:45 -07:00
5 changed files with 38 additions and 38 deletions

View file

@ -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
View file

@ -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

View file

@ -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",

View file

@ -11,3 +11,10 @@ export type Snippet = {
lhs: LHS;
rhs: RHS;
};
export enum SnippetType {
SLSR = 0,
SLMR = 1,
MLSR = 2,
MLMR = 3,
}

View file

@ -1,4 +1,4 @@
import { RHS } from "snippet";
import { RHS } from "src/snippet";
export enum Symbol {
// on parse