<s> import re <EOL> import sublime <EOL> from FuzzyFilePath . common . config import config <EOL> import FuzzyFilePath . common . selection as Selection <EOL> NEEDLE_SEPARATOR = "<STR_LIT>" <EOL> NEEDLE_SEPARATOR_BEFORE = "<STR_LIT>" <EOL> NEEDLE_SEPARATOR_AFTER = "<STR_LIT>" <EOL> NEEDLE_CHARACTERS = "<STR_LIT>" <EOL> NEEDLE_INVALID_CHARACTERS = "<STR_LIT>" <EOL> DELIMITER = "<STR_LIT>" <EOL> def get_context ( view ) : <EOL> error = False <EOL> valid = True <EOL> valid_needle = True <EOL> position = Selection . get_position ( view ) <EOL> word_region = view . word ( position ) <EOL> line_region = view . line ( position ) <EOL> pre_region = sublime . Region ( line_region . a , word_region . a ) <EOL> post_region = sublime . Region ( word_region . b , line_region . b ) <EOL> line = view . substr ( line_region ) <EOL> word = view . substr ( word_region ) <EOL> pre = view . substr ( pre_region ) <EOL> post = view . substr ( post_region ) <EOL> error = re . search ( "<STR_LIT:[>" + NEEDLE_INVALID_CHARACTERS + "<STR_LIT:]>" , word ) <EOL> needle_region = view . word ( position ) <EOL> needle = "<STR_LIT>" <EOL> separator = False <EOL> pre_match = "<STR_LIT>" <EOL> pre_quotes = re . search ( "<STR_LIT>" + NEEDLE_SEPARATOR_BEFORE + "<STR_LIT>" + NEEDLE_SEPARATOR + "<STR_LIT>" , pre ) <EOL> if pre_quotes : <EOL> needle += pre_quotes . group ( <NUM_LIT:2> ) + word <EOL> separator = pre_quotes . group ( <NUM_LIT:1> ) <EOL> pre_match = pre_quotes . group ( <NUM_LIT:2> ) <EOL> needle_region . a -= len ( pre_quotes . group ( <NUM_LIT:2> ) ) <EOL> else : <EOL> pre_quotes = re . search ( "<STR_LIT>" + NEEDLE_SEPARATOR + "<STR_LIT>" , pre ) <EOL> if pre_quotes : <EOL> needle = pre_quotes . group ( <NUM_LIT:2> ) + word <EOL> separator = pre_quotes . group ( <NUM_LIT:1> ) <EOL> pre_match = pre_quotes . group ( <NUM_LIT:2> ) <EOL> needle_region . a -= len ( pre_quotes . group ( <NUM_LIT:2> ) ) <EOL> if pre_quotes : <EOL> post_quotes = re . search ( "<STR_LIT>" + NEEDLE_SEPARATOR_AFTER + "<STR_LIT>" , post ) <EOL> if post_quotes : <EOL> needle += post_quotes . group ( <NUM_LIT:1> ) <EOL> needle_region . b += len ( post_quotes . group ( <NUM_LIT:1> ) ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> valid = False <EOL> elif not re . search ( "<STR_LIT:[>" + NEEDLE_INVALID_CHARACTERS + "<STR_LIT:]>" , needle ) : <EOL> needle = pre + word <EOL> needle_region . a = pre_region . a <EOL> else : <EOL> needle = word <EOL> prefix_region = sublime . Region ( line_region . a , pre_region . b - len ( pre_match ) - <NUM_LIT:1> ) <EOL> prefix_line = view . substr ( prefix_region ) <EOL> prefix = re . search ( "<STR_LIT>" + NEEDLE_CHARACTERS + "<STR_LIT>" + DELIMITER + "<STR_LIT>" , prefix_line ) <EOL> if prefix is None : <EOL> prefix = re . search ( "<STR_LIT>" + NEEDLE_CHARACTERS + "<STR_LIT>" + DELIMITER + "<STR_LIT>" , prefix_line ) <EOL> if prefix : <EOL> prefix = prefix . group ( <NUM_LIT:1> ) <EOL> tag = re . search ( "<STR_LIT>" + NEEDLE_CHARACTERS + "<STR_LIT>" , prefix_line ) <EOL> if tag : <EOL> tag = tag . group ( <NUM_LIT:1> ) <EOL> propertyName = re . search ( "<STR_LIT>" + NEEDLE_CHARACTERS + "<STR_LIT>" , prefix_line ) <EOL> if propertyName : <EOL> propertyName = propertyName . group ( <NUM_LIT:1> ) <EOL> if separator is False : <EOL> valid_needle = False <EOL> valid = False <EOL> elif re . search ( "<STR_LIT:[>" + NEEDLE_INVALID_CHARACTERS + "<STR_LIT:]>" , needle ) : <EOL> valid_needle = False <EOL> valid = False <EOL> elif prefix is None and separator . strip ( ) == "<STR_LIT>" : <EOL> valid = False <EOL> return { <EOL> "<STR_LIT>" : valid , <EOL> "<STR_LIT>" : valid_needle , <EOL> "<STR_LIT>" : needle , <EOL> "<STR_LIT>" : prefix , <EOL> "<STR_LIT>" : tag , <EOL> "<STR_LIT>" : propertyName , <EOL> "<STR_LIT>" : needle_region , <EOL> "<STR_LIT>" : word , <EOL> "<STR_LIT:error>" : error <EOL> } <EOL> def check_trigger ( trigger , expression ) : <EOL> for statement in set ( config [ "<STR_LIT>" ] ) . intersection ( trigger ) : <EOL> values = trigger . get ( statement ) <EOL> if type ( values ) is list and not expression . get ( statement ) in values : <EOL> return False <EOL> return True <EOL> def find_trigger ( expression , scope , triggers ) : <EOL> for trigger in triggers : <EOL> if re . search ( trigger [ "<STR_LIT>" ] , scope ) : <EOL> if check_trigger ( trigger , expression ) : <EOL> return trigger <EOL> return False <EOL> def get_rule ( view ) : <EOL> selection = view . sel ( ) [ <NUM_LIT:0> ] <EOL> position = selection . begin ( ) <EOL> word_region = view . word ( position ) <EOL> current_scope = view . scope_name ( word_region . a ) <EOL> context = get_context ( view ) <EOL> rule = find_rule ( context , current_scope ) <EOL> return [ rule , context ] if rule else False </s>
