"use strict"; const https = require('https'); const pjson = require('../package.json'); import Completers from "./classes/Completers"; import EkoProject from './classes/EkoProject'; import { createConnection, TextDocuments, ProposedFeatures, CompletionItem, TextDocumentPositionParams, TextDocumentChangeEvent } from "vscode-languageserver/lib/main"; const Sentry = require('@sentry/node'); Sentry.init({ dsn: 'https://f298f9ce12e34065a3df8785863a4ecc@sentry.io/33357' }); export let connection = createConnection(ProposedFeatures.all); let documents: TextDocuments = new TextDocuments(); let completers: Completers = new Completers; completers.init().then(() => { let ekoProject: EkoProject = new EkoProject; let projectPath: string = null; let apiCompletions: any = null; let documentUri: string = null; documents.onDidChangeContent(() => { projectPath = ekoProject.getPath(documentUri); if (projectPath) { completers.setPath(projectPath); } }); documents.onDidOpen((e: TextDocumentChangeEvent) => { documentUri = e.document.uri; projectPath = ekoProject.getPath(documentUri); if (projectPath) { fetchDeclarationFile(); } }); function fetchDeclarationFile() { const endpoint = pjson['declaration-file-endpoint']['production']; connection.console.info(`Fetching declarations file...`); https.get(endpoint, (res: any) => { let data = ''; res.on('data', (chunk: any) => { data += chunk; }); res.on('end', () => { connection.console.info(`Successfully fetched declarations file`); apiCompletions = data; if (projectPath) { if (apiCompletions) { ekoProject.addAPICompletions(documentUri, apiCompletions); } } else { connection.console.info(`No project detected`); } }); }).on("error", (err: any) => { connection.console.error(`Failed to fetch declarations file: ${err}`); Sentry.captureException(err); }); }; connection.onInitialize(() => { return { capabilities: { completionProvider: { resolveProvider: false } } }; }); connection.onCompletion( (_textDocumentPosition: TextDocumentPositionParams): CompletionItem[] => { if (projectPath) { return completers.getItems(_textDocumentPosition); } else { return null; } } ); documents.listen(connection); connection.listen(); }).catch((err) => { Sentry.captureException(err); });