private convertToKustoJsSchemaV2()

in package/src/languageServiceManager/kustoLanguageService.ts [1763:1843]


    private convertToKustoJsSchemaV2(schema: s.EngineSchema): GlobalState {
        let cached = this._schemaCache[schema.cluster.connectionString];

        // create a cache entry for the cluster if non yet exists.
        if (!cached) {
            this._schemaCache[schema.cluster.connectionString] = {};
            cached = this._schemaCache[schema.cluster.connectionString];
        }

        // Remove deleted databases from cache
        const schemaDbLookup: { [dbName: string]: s.Database } = schema.cluster.databases.reduce(
            (prev, curr) => (prev[curr.name] = curr),
            {}
        );
        Object.keys(cached).map((dbName) => {
            if (!schemaDbLookup[dbName]) {
                delete cached.dbName;
            }
        });

        let globalState = GlobalState.Default;
        const currentDatabaseName = schema.database ? schema.database.name : undefined;

        let databaseInContext: sym.DatabaseSymbol | undefined = undefined;

        // Update out-of-data databases to cache
        const databases = schema.cluster.databases.map((db) => {
            const shouldIncludeFunctions = db.name === currentDatabaseName;

            const cachedDb = cached[db.name];
            // This is an older version than we have, or we need to parse functions.
            if (
                !cachedDb ||
                cachedDb.database.majorVersion < db.majorVersion ||
                (shouldIncludeFunctions && !cachedDb.includesFunctions)
            ) {
                // only add functions for the database in context (it's very time consuming)

                const databaseSymbol = KustoLanguageService.convertToDatabaseSymbol(db);
                cached[db.name] = { database: db, symbol: databaseSymbol, includesFunctions: shouldIncludeFunctions };
            }

            const databaseSymbol = cached[db.name].symbol;
            if (db.name === currentDatabaseName) {
                databaseInContext = databaseSymbol;
            }

            return databaseSymbol;
        });

        // Replace new URL due to polyfill issue in IE
        // const hostname = new URL(schema.cluster.connectionString.split(';')[0]).hostname;
        const hostname = schema.cluster.connectionString.match(/(.*\/\/)?([^\/;]*)/)[2];
        // safranke.eastus.kusto.windows.net --> safranke.eastus;
        const clusterName = hostname.split(hostname.includes('.kusto') ? '.kusto' : '.')[0];
        const clusterSymbol = new sym.ClusterSymbol.ctor(clusterName, databases);

        globalState = globalState.WithCluster(clusterSymbol);

        if (databaseInContext) {
            globalState = globalState.WithDatabase(databaseInContext);
        }

        // Inject global scalar parameters to global scope.
        const scalarParameters = (schema.globalScalarParameters ?? []).map((param) =>
            KustoLanguageService.createParameterSymbol(param)
        );

        // Inject global tabular parameters to global scope.
        let tabularParameters = (schema.globalTabularParameters ?? []).map((param) =>
            KustoLanguageService.createTabularParameterSymbol(param)
        );

        if (tabularParameters.length || scalarParameters.length) {
            globalState = globalState.WithParameters(
                KustoLanguageService.toBridgeList([...scalarParameters, ...tabularParameters])
            );
        }

        return globalState;
    }