Skip to content
Draft
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
23 changes: 17 additions & 6 deletions src/completionItemBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import ts = require('typescript')
import { adjustMultilineIndentation } from './utils/multiline-expressions'
import { SnippetParser } from 'vscode-snippet-parser'
import { getConfigValue } from './utils'
import { getLastExpressionName, getLastExpressionNode, } from './utils/infer-names'

const RegexExpression = '{{expr(?::(upper|lower|capitalize))?}}'
const RegexExpression = '{{(expr|exprLast|exprRest)(?::(upper|lower|capitalize))?}}'

export class CompletionItemBuilder {
private item: vsc.CompletionItem
Expand Down Expand Up @@ -95,12 +96,22 @@ export class CompletionItemBuilder {
private replaceExpression = (replacement: string, code: string, customRegex?: string) => {
const re = new RegExp(customRegex || RegexExpression, 'g')

return replacement.replace(re, (_match, p1) => {
if (p1 && this.filters[p1]) {
return this.filters[p1](code)
return replacement.replace(re, (_match, type, variant) => {
let codeToInsert: string;
if (type === 'expr') {
codeToInsert = code;
} else if (type === 'exprLast') {
codeToInsert = getLastExpressionName(this.node);
} else {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this explicit instead of an else? Perhaps when there is no match we should throw exception. It should never happen unless you forget to implement one of the options but since there is no strong relation between this and the regex it's impossible to be sure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, but I think I'll be rewriting it a bit to support groups from regexs as we discussed in #90 (comment)

const lastExpr = getLastExpressionNode(this.node)
codeToInsert = lastExpr ? code.slice(0, lastExpr.getStart() - this.node.getStart() - 1) /* -1 for dot */ : ''
}
return code;
})

if (variant && this.filters[variant]) {
return this.filters[variant](codeToInsert)
}
return codeToInsert;
});
}

private filters: {[key: string]: (x: string) => string} = {
Expand Down
26 changes: 17 additions & 9 deletions src/utils/infer-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const inferVarTemplateName = (node: ts.Node): string[] => {
}

export const inferForVarTemplate = (node: ts.Node): string[] => {
const subjectName = getForExpressionName(node)
const subjectName = getLastExpressionName(node)
if (!subjectName) {
return
}
Expand All @@ -47,28 +47,36 @@ function beautifyMethodName(name: string) {
return MethodCallRegex.exec(name)?.groups?.name
}

function getForExpressionName(node: ts.Node) {
export function getLastExpressionNode(node: ts.Node) {
if (ts.isIdentifier(node)) {
return node.text
return node
} else if (ts.isPropertyAccessExpression(node)) {
return node.name.text
return node.name
} else if (ts.isCallExpression(node)) {
return getMethodName(node)
return getMethodNode(node)
}
}

function getMethodName(node: ts.CallExpression) {
export function getLastExpressionName(node: ts.Node) {
return getLastExpressionNode(node)?.text
}

function getMethodNode(node: ts.CallExpression) {
if (ts.isIdentifier(node.expression)) {
return node.expression.text
return node.expression
} else if (ts.isPropertyAccessExpression(node.expression)) {
return node.expression.name.text
return node.expression.name
}
}

function getMethodName(node: ts.CallExpression) {
return getMethodNode(node)?.text
}

function inferNewExpressionVar(node: ts.NewExpression) {
if (ts.isIdentifier(node.expression)) {
return node.expression.text
} else if (ts.isPropertyAccessExpression(node.expression)) {
return node.expression.name.text
}
}
}