import * as path from 'path' ; import { languages , Position , Range , TextDocument , workspace } from "vscode" ; import { IActionContext } from "vscode-azureextensionui" ; import { armTemplateLanguageId , configKeys , configPrefix , documentSchemes } from "../../../common" ; import { containsArmSchema , containsParametersSchema } from "./schemas" ; export const templateDocumentSelector = [ { language : armTemplateLanguageId , scheme : documentSchemes . file } , { language : armTemplateLanguageId , scheme : documentSchemes . untitled } ] ; export const parameterDocumentSelector = [ { language : 'json' , scheme : documentSchemes . file } , { language : 'json' , scheme : documentSchemes . untitled } , { language : 'jsonc' , scheme : documentSchemes . file } , { language : 'jsonc' , scheme : documentSchemes . untitled } , ] ; export const templateOrParameterDocumentSelector = [ { language : armTemplateLanguageId , scheme : documentSchemes . file } , { language : armTemplateLanguageId , scheme : documentSchemes . untitled } , { language : 'json' , scheme : documentSchemes . file } , { language : 'json' , scheme : documentSchemes . untitled } , { language : 'jsonc' , scheme : documentSchemes . file } , { language : 'jsonc' , scheme : documentSchemes . untitled } , ] ; const maxLinesToDetectSchemaIn = 500 ; function isJsonOrJsoncLangId ( textDocument ) { return textDocument . languageId === 'json' || textDocument . languageId === 'jsonc' ; } function shouldWatchDocument ( textDocument ) { if ( textDocument . uri . scheme !== documentSchemes . file && textDocument . uri . scheme !== documentSchemes . untitled && textDocument . uri . scheme !== documentSchemes . git ) { return false ; } if ( textDocument . languageId === armTemplateLanguageId ) { return true ; } return isJsonOrJsoncLangId ( textDocument ) ; } export function isAutoDetectArmEnabled ( ) { return ! ! workspace . getConfiguration ( configPrefix ) . get < boolean > ( configKeys . autoDetectJsonTemplates ) ; } export function mightBeDeploymentTemplate ( textDocument ) { if ( ! shouldWatchDocument ( textDocument ) ) { return false ; } if ( textDocument . languageId === armTemplateLanguageId ) { return true ; } if ( isJsonOrJsoncLangId ( textDocument ) ) { if ( ! isAutoDetectArmEnabled ( ) ) { return false ; } let startOfDocument = textDocument . getText ( new Range ( new Position ( 0 , 0 ) , new Position ( maxLinesToDetectSchemaIn - 1 , 0 ) ) ) ; return ! ! startOfDocument && containsArmSchema ( startOfDocument ) ; } return false ; } export function mightBeDeploymentParameters ( textDocument ) { if ( ! shouldWatchDocument ( textDocument ) ) { return false ; } if ( isJsonOrJsoncLangId ( textDocument ) ) { let startOfDocument = textDocument . getText ( new Range ( new Position ( 0 , 0 ) , new Position ( maxLinesToDetectSchemaIn - 1 , 0 ) ) ) ; return ! ! startOfDocument && containsParametersSchema ( startOfDocument ) ; } return false ; } export function setLangIdToArm ( document , actionContext ) { languages . setTextDocumentLanguage ( document , armTemplateLanguageId ) ; actionContext . telemetry . properties . switchedToArm = 'true' ; actionContext . telemetry . properties . docLangId = document . languageId ; actionContext . telemetry . properties . docExtension = path . extname ( document . fileName ) ; actionContext . telemetry . suppressIfSuccessful = false ; }