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

Custom Templates #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
66 changes: 64 additions & 2 deletions plugins/monaco-wikitext/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ Wikitext completion support

const { monaco } = require("$:/plugins/smilyorg/monaco/monaco.js");
const { LANGUAGE_ID } = require("$:/plugins/smilyorg/monaco-wikitext/language.js");

const STAMP_PREFIX = $tw.wiki.getTiddlerText('$:/config/smilyorg/monaco-wikitext/stampPrefix');

const MATCH_LINK = /(\[\[)([^\|\]]*)(\|?)([^\]]*)(\]?\]?)/;
const MATCH_STAMP = new RegExp(`(${STAMP_PREFIX}${STAMP_PREFIX})([A-Za-z0-9_-]*)`, "");;
const MATCH_TRANSCLUSION = /({{)([^}]*)(}?}?)/;

monaco.languages.registerCompletionItemProvider(LANGUAGE_ID, {
triggerCharacters: ["[", "|", "{"],
triggerCharacters: ["[", "|", "{", STAMP_PREFIX],
resolveCompletionItem(item) {
const title = item.label;
const tiddler = $tw.wiki.getTiddler(title);
Expand Down Expand Up @@ -82,6 +85,27 @@ Wikitext completion support
};
}
}
let templateStart = findNearestDoubleChar(text, textPos, ";");
if (context.triggerKind == monaco.languages.CompletionTriggerKind.TriggerCharacter) {
switch (context.triggerCharacter) {
case ";":
// When triggered by [, ignore if not double [
if (templateStart == -1 || templateStart != textPos-2) {
return null;
}
break;
}
}

if (templateStart != -1) {
const templateSuggestions = getTemplateSuggestions(text, templateStart, textPos, range);
if (templateSuggestions) {
return {
incomplete: true,
suggestions: templateSuggestions,
};
}
}

let transStart = findNearestDoubleChar(text, textPos, "{");
// When triggered by {, ignore if not double {
Expand Down Expand Up @@ -177,10 +201,48 @@ Wikitext completion support
return suggestions;
}

function getTemplateSuggestions(text, start, cursor, range) {
const contents = text.substr(start);
const link = MATCH_STAMP.exec(contents);
if (!link || link.length == 0) {
return null;
}

const end = start + link[0].length;
if (cursor > end) {
return null;
}

let title = link[2];

let titles = $tw.wiki.getTagMap()["$:/stamp"].filter(word => word.includes(title));
const suggestions = [];
for (let i = 0; i < titles.length; i++) {
let title = titles[i];
if (title.lastIndexOf('/') >= 0){
title = title.substr(title.lastIndexOf('/')+1);
}
const insertText = `${title}`;
suggestions.push({
kind: monaco.languages.CompletionItemKind.Field,
label: insertText,
filterText: `;;${title}`,
range: {
startLineNumber: range.startLineNumber,
endLineNumber: range.endLineNumber,
startColumn: range.startColumn + start,
endColumn: range.startColumn + end,
},
insertText: $tw.wiki.getTiddlerText(titles[i]),
})
}

return suggestions;
}

function getTransclusionSuggestions(text, start, cursor, range) {
const contents = text.substr(start);
const trans = MATCH_TRANSCLUSION.exec(contents);
console.log(MATCH_TRANSCLUSION, text, start, contents);
if (!trans || trans.length == 0) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/monaco-wikitext/plugin.info
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"source": "https://github.com/smilyorg/tw5-monaco",
"plugin-type": "plugin",
"parent-plugin": "$:/plugins/smilyorg/monaco",
"list": "readme license"
"list": "readme license stampprefix"
}
3 changes: 3 additions & 0 deletions plugins/monaco-wikitext/stampprefix.tid
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title: $:/config/smilyorg/monaco-wikitext/stampPrefix

;