Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
#22 - Smart code generation for variables and titles.
Browse files Browse the repository at this point in the history
Currently only stores one of each, a variable and a title.
  • Loading branch information
CullieM committed May 25, 2021
1 parent 962a45a commit 924b2d8
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions src/YarnSpinner/yarnSpinnerMonarch.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const tokensWIP =
yarnFloat: /-?[\d]+\.[\d]+/,
yarnInteger: /-?\d+/,
yarnOperator: /(is|==|!=|<=|>=|>(?!>)|<|or|\|\||xor|\^|!|and|&&|\+|-|\*|\/|%|=)/,
dialogueSymbols: /[:!@#%^&*\()\\\|<>?/~`]/,
dialogueSymbols: /[:!@#%^&*\()\\\|<>?/~`,]/,

yarnKeywords: ["as","true","false"],
yarnTypeKeywords: [ "Boolean", "String", "Number"],
Expand Down Expand Up @@ -287,7 +287,7 @@ export const theme = {
};

export const completions = {
provideCompletionItems: () => {
provideCompletionItems: (model, position, context, token) => {
var suggestions = [{
label: 'jump',
kind: monaco.languages.CompletionItemKind.Snippet,
Expand Down Expand Up @@ -352,19 +352,51 @@ export const completions = {
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
}];

var word = "test" //model.getValueInRange({startLineNumber: 1, startColumn: 1, endLineNumber: position.lineNumber, endColumn: position.column});
//Get all of the text in the editor
var text = model.getValueInRange({startLineNumber: 1, startColumn: 1, endLineNumber: position.lineNumber, endColumn: position.column});

var match = word.match(/[A-Za-z_]+[\.]*[A-Za-z_]*/);
//Regex for both titles and variables.
var nodesRegex = /Title:\s?[A-Za-z_]+[\.]*[A-Za-z_]*/;
var variablesRegex = /\$[A-Za-z_]+[\.]*[A-Za-z_]*/;

if(match)
// * FOR FINDING NODE TITLES
var nodes = text.match(nodesRegex);
if(nodes)
{
suggestions.push(
{label: word,
kind: monaco.languages.CompletionItemKind.Snippet,
insertText: word,
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,}
);
}
//Iterate through the array of titles that match.
for(var i = 0; i < nodes.length; i++){
var word = nodes[i];
//Remove the "Title:"
word = word.replace("Title:","");
//Remove any spaces, for example "Title: nodeName"
//Can't use replace("Title: ","") as Title:nodeName is valid afaik.
word = word.replace(" ","");
//Add the word to the completion items.
suggestions.push(
{label: word,
kind: monaco.languages.CompletionItemKind.Class,
insertText: word
}
);
}
}

// * FOR FINDING VARIABLES
var variables = text.match(variablesRegex);
if(variables){
//Iterate through the array of titles that match.
for(var i = 0; i < variables.length; i++){
//Add the word to the completion items.
var word = variables[i];
suggestions.push(
{
label: word,
kind: monaco.languages.CompletionItemKind.Property,
insertText: word
}
);
}
}
return { suggestions: suggestions };
}
};

0 comments on commit 924b2d8

Please sign in to comment.