Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Errors in suggest.ts #355

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update suggest.ts
  • Loading branch information
IsuminI authored Nov 24, 2024
commit cfa45efd050d387cdbc7ddb03552542aa10f69af
55 changes: 33 additions & 22 deletions src/suggest/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class AdmonitionOrCalloutSuggester extends EditorSuggest<
const line = editor.getLine(cursor.line);
const match = this.testAndReturnQuery(line, cursor);
if (!match) return null;
const [_, query] = match;
const [_, prefix, query] = match;

if (
Object.keys(this.plugin.admonitions).find(
Expand All @@ -64,13 +64,12 @@ abstract class AdmonitionOrCalloutSuggester extends EditorSuggest<
return {
end: cursor,
start: {
ch: match.index + this.offset,
ch: match.index + prefix.length,
line: cursor.line
},
query
};
}
abstract offset: number;
abstract selectSuggestion(
value: [string, Admonition],
evt: MouseEvent | KeyboardEvent
Expand All @@ -82,34 +81,35 @@ abstract class AdmonitionOrCalloutSuggester extends EditorSuggest<
}

export class CalloutSuggest extends AdmonitionOrCalloutSuggester {
offset = 4;
selectSuggestion(
[text]: [text: string, item: Admonition],
evt: MouseEvent | KeyboardEvent
): void {
if (!this.context) return;

const line = this.context.editor
.getLine(this.context.end.line)
.slice(this.context.end.ch);
const { editor, query, start, end } = this.context;

const line = editor
.getLine(end.line)
.slice(end.ch);
const [_, exists] = line.match(/^(\] ?)/) ?? [];

this.context.editor.replaceRange(
editor.replaceRange(
`${text}] `,
this.context.start,
start,
{
...this.context.end,
...end,
ch:
this.context.start.ch +
this.context.query.length +
start.ch +
query.length +
(exists?.length ?? 0)
},
"admonitions"
);

this.context.editor.setCursor(
this.context.start.line,
this.context.start.ch + text.length + 2
editor.setCursor(
start.line,
start.ch + text.length + 2
);

this.close();
Expand All @@ -119,32 +119,43 @@ export class CalloutSuggest extends AdmonitionOrCalloutSuggester {
cursor: EditorPosition
): RegExpMatchArray | null {
if (/> ?\[!\w+\]/.test(line.slice(0, cursor.ch))) return null;
if (!/> ?\[!\w*/.test(line)) return null;
return line.match(/> ?\[!(\w*)\]?/);

const match = line.match(/(> ?\[!)(\w*)\]?/);
if (!match) return null;
return match
}
}
export class AdmonitionSuggest extends AdmonitionOrCalloutSuggester {
offset = 6;
selectSuggestion(
[text]: [text: string, item: Admonition],
evt: MouseEvent | KeyboardEvent
): void {
if (!this.context) return;

this.context.editor.replaceRange(
const { editor, start, end } = this.context;

editor.replaceRange(
`${text}`,
this.context.start,
this.context.end,
start,
end,
"admonitions"
);

editor.setCursor(
start.line,
start.ch + text.length
);

this.close();
}
testAndReturnQuery(
line: string,
cursor: EditorPosition
): RegExpMatchArray | null {
if (!/```ad-\w*/.test(line)) return null;
return line.match(/```ad-(\w*)/);

const match = line.match(/(```ad-)(\w*)/);
if (!match) return null;
return match;
}
}