Skip to content

Commit

Permalink
Merge pull request #31 from williamthome/feat/template_completions
Browse files Browse the repository at this point in the history
feat: template completions
  • Loading branch information
williamthome authored Jun 18, 2022
2 parents 7bb3e20 + 42b256a commit f5d365f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
10 changes: 5 additions & 5 deletions snippets/tpl-tags-builtin.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"scope": "tpl",
"description": "{% all catinclude %}",
"prefix": "all catinclude",
"body": "{% all catinclude \"$0.tpl\" %}"
"body": "{% all catinclude \"$1\" %}"
},
"all include": {
"scope": "tpl",
"description": "{% all include %}",
"prefix": "all include",
"body": "{% all include \"$0.tpl\" %}"
"body": "{% all include \"$1\" %}"
},
"autoescape": {
"scope": "tpl",
Expand Down Expand Up @@ -57,7 +57,7 @@
"scope": "tpl",
"description": "{% catinclude %}",
"prefix": "catinclude",
"body": "{% catinclude \"$0.tpl\" %}"
"body": "{% catinclude \"$1\" %}"
},
"comment": {
"scope": "tpl",
Expand Down Expand Up @@ -97,7 +97,7 @@
"scope": "tpl",
"description": "{% extends %}",
"prefix": "extends",
"body": "{% extends \"$0.tpl\" %}"
"body": "{% extends \"$1\" %}"
},
"filter": {
"scope": "tpl",
Expand Down Expand Up @@ -223,7 +223,7 @@
"scope": "tpl",
"description": "{% include %}",
"prefix": "include",
"body": "{% include \"$0.tpl\" %}"
"body": "{% include \"$1\" %}"
},
"inherit": {
"scope": "tpl",
Expand Down
23 changes: 23 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,29 @@ export function activate(context: vscode.ExtensionContext) {
const tplRe = /(?<={(%|{|#|_)?).*/;
const tplRange = document.getWordRangeAtPosition(position, tplRe);
if (tplRange && !tplRange.isEmpty) {
// TODO: If is include, provide vars completion.
const templateRe = /(?<={%\s*(extends|(all\s*)?((cat)?include))\s*\").*?(?=")/;
const templateRange = document.getWordRangeAtPosition(position, templateRe);
if (!!templateRange) {
const templatesPattern = "{apps,apps_user}/**/priv/templates/**/*.tpl";
const templates = await vscode.workspace.findFiles(templatesPattern);
return templates.reduce((arr, file) => {
const filePathRe = /(?<=\/priv\/templates\/).*/;
const filePathMatch = filePathRe.exec(file.fsPath);
if (!filePathMatch || !filePathMatch.length) {
return arr;
}
const filePath = filePathMatch[0];
if (!arr.some((s) => s.label === filePath)) {
const snippet = new vscode.CompletionItem(filePath);
snippet.insertText = new vscode.SnippetString(filePath);

arr.push(snippet);
}
return arr;
}, new Array<vscode.CompletionItem>());
}

const variableRe = /(?<=\[).*?(?=\])|(?<={).*?(?=})|(?<=:).*?(?=}|,)|(?<==).*?(?=(%}|,|}))/;
const variableRange = document.getWordRangeAtPosition(position, variableRe);
if (!!variableRange) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/test.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{% extends "" %}
{% include "" %}
{% catinclude "" %}
{% all include "" %}
{% all catinclude "" %}

<a href="{% url admin_edit_rsc id=42 foo="bar" absolute_url %}"></a>

<h1 class="title {% if m.req.csp_nonce as foo %}{{ foo }}{% else %}bar{% endif %}">My title</h1>
Expand Down

0 comments on commit f5d365f

Please sign in to comment.