import * as path from "path" ; import * as vscode from "vscode" ; import type { GlobalStringInterface } from "./data/globalstring/GlobalStringInterface" ; const events = { data : require ( "./data/event" ) . data , hover : require ( "./providers/event" ) . getHover , } ; const cvars = { data : require ( "./data/cvar" ) . data , hover : require ( "./providers/cvar" ) . getHover , } ; import enum_provider = require ( "./providers/enum" ) ; const enums = { data : require ( "./data/enum" ) . data , completion : enum_provider . completion , hover : enum_provider . getHover , } ; import globalstring_provider = require ( "./providers/globalstring" ) ; const globalstrings = { data : { } as GlobalStringInterface , completion : globalstring_provider . completion , hover : globalstring_provider . getHover , } ; function isHoverString ( document , range ) { if ( range . start . character > 0 ) { const leftPos = new vscode . Position ( range . start . line , range . start . character - 1 ) ; const rightPos = new vscode . Position ( range . end . line , range . end . character + 1 ) ; const word = document . getText ( new vscode . Range ( leftPos , rightPos ) ) ; const leftChar = word . charAt ( 0 ) ; const rightChar = word . charAt ( word . length - 1 ) ; return ( leftChar == '"' && rightChar == '"' ) || ( leftChar == "'" && rightChar == "'" ) ; } return false ; } export function activate ( context ) { console . log ( "loaded ketho.wow-api" , context . extension . id ) ; setExternalLibrary ( "API" , true ) ; const loadFrameXML = vscode . workspace . getConfiguration ( "wowAPI" ) . get ( "emmyLua.loadFrameXML" ) ; setExternalLibrary ( "Optional" , loadFrameXML ? true : false ) ; updateConfigs ( ) ; const completion = vscode . languages . registerCompletionItemProvider ( "lua" , { provideCompletionItems ( document , position ) { const linePrefix = document . lineAt ( position ) . text . substr ( 0 , position . character ) ; const lastWord = linePrefix . split ( /[^\w\.]/ ) . slice ( - 1 ) [ 0 ] ; if ( lastWord . startsWith ( "LE_" ) ) return enums . completion ; else if ( lastWord . length > 3 && lastWord == lastWord . match ( "^[0-9A-Z_]+" ) ?. [ 0 ] ) { return globalstrings . completion ; } } } , "_" , "A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" , "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "W" , "X" , "Y" , "Z" ) ; onCustomCompletion ( ) ; const hover = vscode . languages . registerHoverProvider ( "lua" , { provideHover ( document , position ) { const range = document . getWordRangeAtPosition ( position ) ; if ( range ) { const word = document . getText ( range ) ; const lword = word . toLowerCase ( ) ; if ( events . data [ word ] ) return events . hover ( word ) ; else if ( cvars . data [ lword ] && isHoverString ( document , range ) ) return cvars . hover ( lword ) ; else if ( enums . data [ word ] ) return enums . hover ( word ) ; else if ( globalstrings . data [ word ] ) return globalstrings . hover ( word ) ; } } } ) ; context . subscriptions . push ( completion , hover ) ; vscode . workspace . onDidChangeConfiguration ( ( event ) => { if ( event . affectsConfiguration ( "wowAPI.emmyLua.loadFrameXML" ) ) { const loadFrameXML = vscode . workspace . getConfiguration ( "wowAPI" ) . get ( "emmyLua.loadFrameXML" ) ; setExternalLibrary ( "Optional" , loadFrameXML ? true : false ) ; } else if ( event . affectsConfiguration ( "wowAPI.locale" ) ) { const wowapi = vscode . workspace . getConfiguration ( "wowAPI" ) ; globalstrings . data = require ( "./data/globalstring/" + wowapi . get ( "locale" ) ) . data ; } } ) ; } export function deactivate ( ) { console . log ( "deactivated ketho.wow-api" ) ; setExternalLibrary ( "API" , false ) ; setExternalLibrary ( "Optional" , false ) ; } export function setExternalLibrary ( folder , enable ) { console . log ( "setExternalLibrary" , folder , enable ) ; const extensionId = "ketho.wow-api" ; const extensionPath = vscode . extensions . getExtension ( extensionId ) ?. extensionPath ; const folderPath = path . join ( extensionPath ! , "EmmyLua" , folder ) ; const config = vscode . workspace . getConfiguration ( "Lua" ) ; const library = config . get ( "workspace.library" ) ; if ( library && extensionPath ) { for ( let i = library . length - 1 ; i >= 0 ; i -- ) { const el = library [ i ] ; const isSelfExtension = el . indexOf ( extensionId ) > - 1 ; const isCurrentVersion = el . indexOf ( extensionPath ) > - 1 ; if ( isSelfExtension && ! isCurrentVersion ) { library . splice ( i , 1 ) ; } } const index = library . indexOf ( folderPath ) ; if ( enable ) { if ( index == - 1 ) { library . push ( folderPath ) ; } } else { if ( index > - 1 ) { library . splice ( index , 1 ) ; } } config . update ( "workspace.library" , library , true ) ; } } function updateConfigs ( ) { const sumneko = vscode . workspace . getConfiguration ( "Lua" ) ; sumneko . update ( "completion.displayContext" , 0 , true ) ; const wowapi = vscode . workspace . getConfiguration ( "wowAPI" ) ; globalstrings . data = require ( "./data/globalstring/" + wowapi . get ( "locale" ) ) . data ; } function onCustomCompletion ( ) { vscode . commands . registerTextEditorCommand ( "ketho.wow-api.onCompletion" , ( editor ) => { const pos = editor . selection . active ; const range = editor . document . getWordRangeAtPosition ( pos ) ; const word = editor . document . getText ( range ) ; const isValidWord = enums . data [ word ] || globalstrings . data [ word ] ; const config = vscode . workspace . getConfiguration ( "Lua" ) ; const globals = config . get ( "diagnostics.globals" ) ; if ( isValidWord && globals && globals . indexOf ( word ) == - 1 ) { globals . push ( word ) ; config . update ( "diagnostics.globals" , globals ) ; } } ) ; }