<s> import sublime <EOL> import sublime_plugin <EOL> import os <EOL> import re <EOL> import fnmatch <EOL> import json <EOL> from . . ml_utils import MlUtils <EOL> from . . ml_settings import MlSettings <EOL> class MarkLogicAutoComplete ( sublime_plugin . EventListener ) : <EOL> def __init__ ( self ) : <EOL> self . dynamic_snippets = None <EOL> self . xquery_function_snippets = [ ] <EOL> self . javascript_function_snippets = [ ] <EOL> def gen_dynamic_snippets ( self ) : <EOL> if ( self . dynamic_snippets == None ) : <EOL> self . dynamic_snippets = [ ] <EOL> snip_dir = "<STR_LIT>" <EOL> for filename in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> f = MlUtils . load_resource ( os . path . join ( snip_dir , filename ) ) <EOL> jo = json . loads ( f ) <EOL> if isinstance ( jo , list ) : <EOL> for snip in jo : <EOL> self . dynamic_snippets . append ( self . create_snippet_object ( snip ) ) <EOL> else : <EOL> self . dynamic_snippets . append ( self . create_snippet_object ( jo ) ) <EOL> def gen_function_snippets ( self , snippets , filename ) : <EOL> if ( len ( snippets ) == <NUM_LIT:0> ) : <EOL> functions_file = "<STR_LIT>" % filename <EOL> f = MlUtils . load_resource ( functions_file ) <EOL> for s in json . loads ( f ) : <EOL> snippets . append ( self . create_snippet_object ( s ) ) <EOL> def create_snippet_object ( self , snip ) : <EOL> completion = snip [ '<STR_LIT>' ] <EOL> if ( '<STR_LIT:description>' in snip ) : <EOL> completion = completion + '<STR_LIT:\t>' + snip [ '<STR_LIT:description>' ] <EOL> o = { <EOL> '<STR_LIT>' : snip [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : completion , <EOL> '<STR_LIT:content>' : snip [ '<STR_LIT:content>' ] <EOL> } <EOL> return o <EOL> def get_module_namespace ( self , view ) : <EOL> contents = view . substr ( sublime . Region ( <NUM_LIT:0> , view . size ( ) ) ) <EOL> search = re . search ( r"<STR_LIT>" , contents , re . MULTILINE ) <EOL> if search != None : <EOL> return search . groups ( ) [ <NUM_LIT:0> ] <EOL> return '<STR_LIT>' <EOL> def process_dynamic_snippets ( self , view , prefix , completions ) : <EOL> self . gen_dynamic_snippets ( ) <EOL> namespace = self . get_module_namespace ( view ) <EOL> for snip in self . dynamic_snippets : <EOL> trigger = snip [ '<STR_LIT>' ] <EOL> if ( prefix in trigger ) : <EOL> content = re . sub ( r'<STR_LIT>' , namespace , snip [ '<STR_LIT:content>' ] ) <EOL> completions . append ( ( snip [ '<STR_LIT>' ] , content ) ) <EOL> def process_function_snippets ( self , view , prefix , snippets , filename , completions ) : <EOL> self . gen_function_snippets ( snippets , filename ) <EOL> if MlSettings . enable_marklogic_functions ( ) == True : <EOL> for snip in snippets : <EOL> trigger = snip [ '<STR_LIT>' ] <EOL> if ( prefix in trigger ) : <EOL> content = snip [ '<STR_LIT:content>' ] <EOL> completions . append ( ( snip [ '<STR_LIT>' ] , content ) ) <EOL> def snippets_from_xqy_file ( self , file_name , contents , ns_prefix , show_private , prefix , completions ) : <EOL> for func in MlUtils . get_function_defs ( file_name , contents , ns_prefix , show_private ) : <EOL> trigger = func [ <NUM_LIT:0> ] <EOL> if ( prefix in trigger ) : <EOL> description = "<STR_LIT>" % re . sub ( r"<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:U+002C>" . join ( func [ <NUM_LIT:1> ] ) ) <EOL> completion = "<STR_LIT>" % ( trigger , description ) <EOL> params = [ ] <EOL> index = <NUM_LIT:1> <EOL> for param in func [ <NUM_LIT:1> ] : <EOL> params . append ( '<STR_LIT>' % ( index , param ) ) <EOL> index = index + <NUM_LIT:1> <EOL> content = "<STR_LIT>" % ( trigger , "<STR_LIT:U+002CU+0020>" . join ( params ) ) <EOL> completions . append ( ( completion , content ) ) <EOL> def process_included_code_snippets ( self , view , prefix , completions ) : <EOL> contents = view . substr ( sublime . Region ( <NUM_LIT:0> , view . size ( ) ) ) <EOL> file_name = view . file_name ( ) <EOL> ns_prefix , ns_uri = MlUtils . get_namespace ( contents ) <EOL> self . snippets_from_xqy_file ( file_name , contents , ns_prefix , True , prefix , completions ) <EOL> if ( file_name ) : <EOL> for other_file , ns_prefix in MlUtils . get_imported_files ( file_name , contents ) : <EOL> with open ( other_file , "<STR_LIT:r>" ) as myfile : <EOL> buffer = myfile . read ( ) <EOL> self . snippets_from_xqy_file ( other_file , buffer , ns_prefix , False , prefix , completions ) <EOL> def on_query_completions ( self , view , prefix , locations ) : <EOL> completions = [ ] <EOL> if view . match_selector ( locations [ <NUM_LIT:0> ] , "<STR_LIT>" ) : <EOL> self . process_dynamic_snippets ( view , prefix , completions ) <EOL> self . process_function_snippets ( view , prefix , self . xquery_function_snippets , '<STR_LIT>' , completions ) <EOL> self . process_included_code_snippets ( view , prefix , completions ) <EOL> elif MlUtils . is_server_side_js ( view ) : <EOL> self . process_function_snippets ( view , prefix , self . javascript_function_snippets , '<STR_LIT>' , completions ) <EOL> return completions </s>
