private getExtractVarParamCodeActions()

in src/documents/templates/DeploymentTemplateDoc.ts [613:671]


    private getExtractVarParamCodeActions(range: Range | Selection): (CodeAction | Command)[] {
        let shouldAddExtractActions: boolean = false;
        let pc = this.getContextFromDocumentLineAndColumnIndexes(range.start.line, range.start.character, undefined);

        // We currently only handle single-line strings
        if (range.start.line === range.end.line) {
            // Can only call pc.jsonTokenStartIndex if we're inside a JSON token
            if (pc.jsonToken && pc.jsonTokenStartIndex > 0) {
                let jsonToken = pc.document.getJSONValueAtDocumentCharacterIndex(pc.jsonTokenStartIndex - 1, ContainsBehavior.extended);
                if ((jsonToken instanceof Json.Property || jsonToken instanceof Json.ArrayValue) && pc.document.topLevelValue) {
                    let scope = pc.getScope();
                    if (!scope.rootObject) {
                        return [];
                    }
                    let resources = scope.rootObject.getPropertyValue(templateKeys.resources);
                    // Are we inside the resources object?
                    if (!resources || !resources.span.intersect(jsonToken.span)) {
                        return [];
                    }
                    let jsonValue = pc.document.getJSONValueAtDocumentCharacterIndex(pc.jsonTokenStartIndex, ContainsBehavior.extended);
                    if (!jsonValue) {
                        return [];
                    }
                    const stringValue = jsonValue.asStringValue;
                    if (stringValue) {
                        if (!range.isEmpty) {
                            let startIndex = this.getDocumentCharacterIndex(range.start.line, range.start.character);
                            let endIndex = this.getDocumentCharacterIndex(range.end.line, range.end.character);
                            let span: Span = new Span(startIndex, endIndex - startIndex);
                            const selectedText = this.getDocumentTextWithSquareBrackets(span);
                            if (this.isParameterOrVariableReference(selectedText)) {
                                return [];
                            }
                            if (pc.jsonValue && jsonValue.span === pc.jsonValue.span && selectedText && this.equalsWithSqareBrackets(pc.jsonValue.asStringValue?.unquotedValue, selectedText)) {
                                shouldAddExtractActions = true;
                            } else {
                                if (isTleExpression(stringValue.unquotedValue)) {
                                    shouldAddExtractActions = this.isValidExpression(this.getDocumentTextWithSurroundingCharacters(span, "'", "'"));
                                }
                            }
                        } else {
                            if (this.isSimpleText(stringValue.quotedValue) && pc.jsonValue && jsonValue.span === pc.jsonValue.span) {
                                shouldAddExtractActions = true;
                            }
                        }
                    }
                }
            }
        }

        if (shouldAddExtractActions) {
            return [
                this.createExtractCommand('Extract Parameter...', 'extractParameter'),
                this.createExtractCommand('Extract Variable...', 'extractVariable'),
            ];
        }

        return [];
    }