import * as vscode from 'vscode' ; import { LanguageClient , LanguageClientOptions , ServerOptions } from 'vscode-languageclient' ; import { Logger } from '../util/logger' ; import { Configuration } from '../util/configuration' ; import { Python } from '../util/python' ; import { DocumentLinkProvider } from './docLinkProvider' ; import open = require ( 'open' ) ; import mime = require ( 'mime' ) ; import { StatusBarAlignment , window , workspace } from 'vscode' ; export async function activate ( context , logger , disabled , python ) { if ( disabled ) { return ; } if ( vscode . workspace . workspaceFolders && vscode . workspace . workspaceFolders . length > 1 ) { vscode . window . showWarningMessage ( 'IntelliSense is not available. Snooty language server does not support multi-root workspaces.' ) ; return ; } if ( ! ( await python . checkPython ( null , false ) ) || ! ( await python . checkPythonForSnooty ( ) ) ) { vscode . window . showErrorMessage ( 'Python is not installed, or its version is too old. Snooty language server requires 3.7 and above.' ) ; return ; } context . subscriptions . push ( vscode . workspace . onDidChangeConfiguration ( ( ) => { } ) ) ; let serverModule = await Configuration . getPythonPath ( ) ; let args = [ ] ; let options = { } ; const sourceFolder = Configuration . getSnootySourceFolder ( ) ; if ( sourceFolder ) { options . cwd = sourceFolder ; if ( await python . checkPython ( null , false ) && await python . checkSnooty ( null , false , false ) ) { await python . uninstallSnooty ( ) ; vscode . window . showInformationMessage ( 'Uninstalled snooty-lextudio.' ) ; } if ( ! ( await python . checkPython ( null , false ) ) || ! ( await python . checkDebugPy ( null , true ) ) ) { return ; } args . push ( '-m' , 'debugpy' , '--listen' , '5678' ) ; if ( Configuration . getSnootyDebugLaunch ( ) ) { args . push ( '--wait-for-client' ) ; } vscode . window . showInformationMessage ( 'debugpy is at port 5678. Connect and debug.' ) ; } else { if ( ! ( await python . checkPython ( null , false ) ) || ! ( await python . checkSnooty ( null , true , true ) ) ) { return ; } } logger . log ( 'Use Snooty language server' ) ; args . push ( '-m' , 'snooty' , 'language-server' ) ; if ( serverModule != null ) { let serverOptions = { run : { command : serverModule , args : args , options : options } , debug : { command : serverModule , args : args , options : options } } ; const documentSelector = [ { language : 'restructuredtext' , scheme : 'file' } , ] ; let clientOptions = { documentSelector , synchronize : { configurationSection : 'snooty' , fileEvents : [ vscode . workspace . createFileSystemWatcher ( '**/*.rst' ) , ] } } ; const client = new LanguageClient ( 'Snooty Language Client' , serverOptions , clientOptions ) ; const restartServer = vscode . commands . registerCommand ( 'snooty.restart' , async ( ) => { vscode . commands . executeCommand ( 'workbench.action.reloadWindow' ) ; } ) ; ( ( ) => { let config = workspace . getConfiguration ( 'snooty' ) ; let statusStyle = config . get ( 'misc.status' , 'short' ) ; if ( statusStyle === 'short' || statusStyle === 'detailed' ) { let statusIcon = window . createStatusBarItem ( StatusBarAlignment . Right ) ; statusIcon . command = 'snooty.restart' ; statusIcon . text = 'snooty: loading' ; statusIcon . tooltip = 'Click to reload window and restart Snooty language server' ; statusIcon . show ( ) ; client . onReady ( ) . then ( ( ) => { statusIcon . text = 'snooty: idle' ; vscode . languages . registerDocumentLinkProvider ( documentSelector , new DocumentLinkProvider ( client ) ) ; } ) ; } } ) ( ) ; context . subscriptions . push ( client . start ( ) ) ; context . subscriptions . push ( restartServer ) ; let hoverFile ; const clickInclude = vscode . commands . registerCommand ( 'snooty.clickInclude' , async ( ) => { const type = mime . getType ( hoverFile ) ; if ( type == null || ! type . includes ( "image" ) ) { const textDoc = await vscode . workspace . openTextDocument ( hoverFile ) ; vscode . window . showTextDocument ( textDoc ) ; } else { open ( hoverFile ) ; } } ) ; context . subscriptions . push ( clickInclude ) ; } }