Skip to content

Commit

Permalink
Sample template explained.
Browse files Browse the repository at this point in the history
More complete set of completions for Sitecore functions and field/item accessors.
  • Loading branch information
AdamNaj committed Aug 6, 2019
1 parent 10109bf commit b466670
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 66 deletions.
24 changes: 14 additions & 10 deletions .examples/gallery.scriban
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
{{ if sc_evaluate i_item "Is Image" # example of rule evaluation }}
{{ sc_editframe i_item "Gallery Image" # example of edit frame }}
{{- # ↓ Name of the item template -}}
{{ if i_item.template_name == "Gallery Image" }}
<a class="field-image">
<a title="{{ i_item.ImageTitle.Raw }}" {{ # raw value from a field }} href="{{i_item.Image.media_url }}"> {{ # get link from a linked media item }}
{{ sc_field i_item ["Link2", "Image2", "Image"] [["data-2",2],[3,4], "ssss"] }}

{{- # ↓ raw value from the field ↓ media url from a field -}}
<a title="{{ i_item.ImageTitle.raw }}" href="{{ i_item.Image.media_url }}">

{{- # ↓ item.FieldName - renders Sitecore field -}}
{{ i_item.Image }}
</a>
</a>
{{ sc_endeditframe }}
{{end}}

{{- # ↓ conditional statement }}
{{ if i_item.template_name == "Gallery Video" }}
{{ sc_editframe i_item "Gallery Video" }}
{{ if (i_item.VideoID.raw ) == "" && (i_item.VideoThumbnail.raw) == "" }}
<span>[Edit Gallery Video here]</span>
{{ else }}
<a title="{{ i_item.VideoTitle.raw }}" href="http://www.youtube.com/watch?v={{ i_item.VideoID.raw }}">
<img src="{{ o_model.thumbnail_url }}" alt="{{ i_item.VideoDescription.raw }}"
<a title="{{i_item.VideoTitle.raw }}" href="http://www.youtube.com/watch?v={{ i_item.VideoID.raw }}">

{{- # ↓ access model properties }}
<img src="{{ o_model.thumbnail_url }}" alt="{{ i_item.VideoDescription.raw }}"
data-title="{{ i_item.VideoTitle.raw }}" data-description="{{ i_item.VideoDescription.raw }}" ></img>
</a>
{{end}}
{{ sc_endeditframe }}
{{end}}
{{end}}
13 changes: 13 additions & 0 deletions .examples/promo.scriban
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="field-promoicon">
{{ i_item.PromoIcon }}
</div>
<div class="promo-text">
<div>
<div class="field-promotext">
{{ i_item.PromoText }}
</div>
</div>
<div class="field-promolink">
{{ i_item.PromoLink }}
</div>
</div>
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"args": [
"--extensionDevelopmentPath=${workspaceRoot}",
"${workspaceFolder}\\.examples\\gallery.scriban",
"${workspaceFolder}\\.examples\\promo.scriban",
"${workspaceFolder}\\.examples\\empty.scriban"
],
"outFiles": [
Expand Down
55 changes: 27 additions & 28 deletions src/autoCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import * as vscode from 'vscode';
import { languageObjects } from './languageObjects';
import { itemMethods, embeddedItemCompletions, fieldMethods } from './sitecoreObjects';
import { methods, embeddedItemCompletions } from './sitecoreObjects';
import { sitecoreFunctions } from './sitecoreFunctions';
import { language } from './languageSyntax';
import { isInFunctionCompletion, codeBlockFromTemplate, stripFunctionNameFromLine, isInFieldCompletion, isInScriban } from './regularExpressions';
import { isInFunctionCompletion, codeBlockFromTemplate, stripFunctionNameFromLine, isInScriban } from './regularExpressions';
import { snippetCompletion, objectFunctionCompletion } from './autoCompletionItem';

const documentStart = new vscode.Position(0, 0);
Expand All @@ -16,7 +16,12 @@ export function getCodeBlockFromSnippet(snippet: string, includeBrackets: boolea

let codeBlock = snippet.replace(codeBlockFromTemplate, "$2");

if (includeBrackets) {
return getInsertBlockFromSnippet(codeBlock,includeBrackets);
}

export function getInsertBlockFromSnippet(codeBlock: string, includeBrackets: boolean = true) {

if (includeBrackets) { // || codeBlock.indexOf("{{") > -1 || codeBlock.indexOf("}}") > -1) {
return "{{ " + codeBlock + " }}";
} else {
return codeBlock;
Expand Down Expand Up @@ -96,31 +101,25 @@ export function provideFunctionCompletionItems(document: vscode.TextDocument, po
}
}

// embedded variables: i_item, i_home, i_datasource ...
for (let item of embeddedItemCompletions) {
const commitCharacterCompletion = new vscode.CompletionItem(item.name);
commitCharacterCompletion.kind = vscode.CompletionItemKind.Variable;
commitCharacterCompletion.insertText = item.name;
commitCharacterCompletion.commitCharacters = ['.'];
commitCharacterCompletion.documentation = new vscode.MarkdownString(item.description);
commitCharacterCompletion.command = { command: 'editor.action.triggerSuggest', title: 'Re-trigger completions...' };
results.push(commitCharacterCompletion);
}

// embedded item functions
if (isInFunctionCompletion.test(linePrefix)) {
var prefix = "i_item.";
itemMethods.forEach(element => {
results.push(objectFunctionCompletion(prefix, element.name, element.template, element.description, vscode.CompletionItemKind.Property))
});
}

// embedded item functions and field functions
if (isInFieldCompletion.test(linePrefix)) {
var prefix = "i_item.field.";
fieldMethods.forEach(element => {
results.push(objectFunctionCompletion(prefix, element.name, element.template, element.description, vscode.CompletionItemKind.Property))
});
// properties for embedded objects
methods.forEach(element => {
var validationRegex = new RegExp(element.validationRegEx);
if (validationRegex.test(linePrefix)) {
results.push(objectFunctionCompletion(element.prefix, element.name, element.template, element.description, vscode.CompletionItemKind.Property))
}
});

// embedded objects: i_item, i_home, i_datasource ...
if (!isInFunctionCompletion.test(linePrefix)) {
for (let item of embeddedItemCompletions) {
const commitCharacterCompletion = new vscode.CompletionItem(item.name);
commitCharacterCompletion.kind = vscode.CompletionItemKind.Variable;
commitCharacterCompletion.insertText = item.name;
commitCharacterCompletion.commitCharacters = ['.'];
commitCharacterCompletion.documentation = new vscode.MarkdownString(item.description);
commitCharacterCompletion.command = { command: 'editor.action.triggerSuggest', title: 'Re-trigger completions...' };
results.push(commitCharacterCompletion);
}
}

return results;
Expand Down
4 changes: 2 additions & 2 deletions src/languageObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const languageObjects = [
{
"name": "add_range",
"template": "add_range ${1:<list1>} ${2:<list2>}",
"description": "Appends <__list2__> to another list from pipeline",
"description": "Appends <__list2__> to list <__list1__> and return the resulting list",
"pipeTemplate": "add_range ${1:<list2>}",
},
{
Expand All @@ -27,7 +27,7 @@ export const languageObjects = [
{
"name": "concat",
"template": "concat ${1:<list2>}",
"description": "Appends <__list2__> to another list from pipeline",
"description": "Appends <__list2__> to list <__list1__> and return the resulting list",
"pipeTemplate": "concat ${1:<list1>} ${2:<list2>}",
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/languageSyntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const language: ScribanSnippet[] = [
"description": "For loop, iterating over collection",
"validationRegEx": "",
"template": [
"{{ for ${1:item} in ${2:collection} }}",
"{{ for ${1:var} in ${2:collection} }}",
"\t$3",
"{{ end }}"
],
Expand Down
14 changes: 0 additions & 14 deletions src/regularExpressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,3 @@ export const codeBlockFromTemplate = new RegExp(/(\$[{]*\d+[:|]*)([\w\s-\.]*)([
*/

export const snippetVariableCleanup = new RegExp(/(\${\d+:)|(})/g);

export const isInItemCompletion = new RegExp(/(i_[\w.]*)$/g);
/* regex test data
i_item.parent
i_item.parent.name
*/

export const isInFieldCompletion = new RegExp(/(i_\w*\.\w*\.[\w\.]*)$/g);
/* regex test data
i_item.parent.name
i_item.parent.
*/
66 changes: 61 additions & 5 deletions src/sitecoreFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,29 @@ export const sitecoreFunctions = [
"description": "Outputs component decoration with styles, grid classes and attributes needed by creative exchange",
"validationRegEx": "",
"template": "sc_decorate",
"pipeTemplate": "",
"pipeTemplate": "sc_decorate",
},
{
"name": "sc_decorate",
"description": "Outputs component decoration with styles, grid classes and attributes needed by creative exchange",
"validationRegEx": "",
"template": "sc_decorate ['${1:css-class}']",
"pipeTemplate": "",
"pipeTemplate": "sc_decorate",
},
{
"name": "sc_translate",
"description": "Translate a label to the context language or the language specified as a parameter using the site dictionary.",
"description": "Translate a label to the language specified through the parameter using the site dictionary.",
"validationRegEx": "",
"template": "sc_translate '${1:phrase}' ${2:'languagename'?}",
"template": "sc_translate '${1:phrase}' ${2:'optional-languagename'?}",
"pipeTemplate": "sc_translate ${1:\"languagename\"}",
},
{
"name": "sc_translate",
"description": "Translate a label to the context language using the site dictionary.",
"validationRegEx": "",
"template": "sc_translate '${1:phrase}'",
"pipeTemplate": "sc_translate",
},
{
"name": "sc_editframe",
"description": "Wrapping block for an edit frame",
Expand All @@ -56,7 +63,7 @@ export const sitecoreFunctions = [
},
{
"name": "sc_execute",
"description": "Renders variant beneath the current script providing item to the pipeline",
"description": "Renders variant beneath the current script providing <__item__> to the pipeline",
"validationRegEx": "",
"template": "sc_execute ${1|i_item,i_datasource,i_home,i_page,i_site|} '${2:SubVariantName}'",
"pipeTemplate": "sc_execute '${1:SubVariantName}'",
Expand All @@ -68,6 +75,55 @@ export const sitecoreFunctions = [
"template": "sc_evaluate ${1|i_item,i_datasource,i_home,i_page,i_site|} '${2:Rule Name}'",
"pipeTemplate": "sc_evaluate '${1:Rule Name}'",
},
{
"name": "sc_raw",
"description": "Retrieves the raw value of a field from item",
"validationRegEx": "",
"template": "sc_raw ${1|i_item,i_datasource,i_home,i_page,i_site|} '${2:Field Name}'",
"pipeTemplate": "sc_raw '${1:Field Name}'",
},
{
"name": "sc_follow",
"description": "Retrieves an item linked in a field. If the field can link to multiple items - the first one is returned. If no items are linked, returns null.",
"validationRegEx": "",
"template": "sc_follow ${1|i_item,i_datasource,i_home,i_page,i_site|} '${2:Field Name}'",
"pipeTemplate": "sc_follow '${1:Field Name}'",
},
{
"name": "sc_followmany",
"description": "Retrieves the collection of items linked in a field. If no items are linked, returns null.",
"validationRegEx": "",
"template": "sc_followmany ${1|i_item,i_datasource,i_home,i_page,i_site|} '${2:Field Name}'",
"pipeTemplate": "sc_followmany '${1:Field Name}'",
},
{
"name": "sc_link",
"description": "Returns the link to the provided item. If item is null, returns '#'",
"validationRegEx": "",
"template": "sc_link ${1|i_item,i_datasource,i_home,i_page,i_site|}",
"pipeTemplate": "sc_link",
},
{
"name": "sc_medialink",
"description": "Returns the media link to the provided item. If item is null, returns '#'",
"validationRegEx": "",
"template": "sc_medialink ${1|i_item,i_datasource|}",
"pipeTemplate": "sc_medialink",
},
{
"name": "sc_query",
"description": "Executes sitecore query relative to the item provided and returns collection of items based on the results of the query.",
"validationRegEx": "",
"template": "sc_query ${1|i_item,i_datasource,i_home,i_page,i_site|} '${2:query:./*}'",
"pipeTemplate": "sc_query '${1:query:./*}'",
},
{
"name": "sc_parameter",
"description": "",
"validationRegEx": "",
"template": "sc_parameter '${1:Parameter Name}'",
"pipeTemplate": "sc_parameter",
},
{
"name": "",
"description": "",
Expand Down
Loading

0 comments on commit b466670

Please sign in to comment.