예제 #1
0
def do_css_reflection(editor):
    content = editor.get_content()
    caret_pos = editor.get_caret_pos()
    css = parser_utils.extract_css_rule(content, caret_pos)

    if not css or caret_pos < css[0] or caret_pos > css[1]:
        # no matching CSS rule or caret outside rule bounds
        return False

    tokens = parser_utils.parse_css(content[css[0]:css[1]], css[0])
    token_ix = find_token_from_position(tokens, caret_pos, 'identifier')

    if token_ix != -1:
        cur_prop = tokens[token_ix]['content']
        value_token = find_value_token(tokens, token_ix + 1)
        base_name = get_base_css_name(cur_prop)
        re_name = re.compile('^(?:\\-\\w+\\-)?' + base_name + '$')
        re_name = get_reflected_css_name(base_name)
        values = []

        if not value_token:
            return False

        # search for all vendor-prefixed properties
        for i, token in enumerate(tokens):
            if token['type'] == 'identifier' and re.search(
                    re_name,
                    token['content']) and token['content'] != cur_prop:
                v = find_value_token(tokens, i + 1)
                if v:
                    values.append({'name': token, 'value': v})

        # some editors do not provide easy way to replace multiple code
        # fragments so we have to squash all replace operations into one
        if values:
            data = content[
                values[0]['value']['start']:values[-1]['value']['end']]
            offset = values[0]['value']['start']
            value = value_token['content']

            values.reverse()
            for v in values:
                rv = get_reflected_value(cur_prop, value, v['name']['content'],
                                         v['value']['content'])
                data = replace_substring(data, v['value']['start'] - offset,
                                         v['value']['end'] - offset, rv)

                # also calculate new caret position
                if v['value']['start'] < caret_pos:
                    caret_pos += len(rv) - len(v['value']['content'])

            return {
                'data': data,
                'start': offset,
                'end': values[0]['value']['end'],
                'caret': caret_pos
            }

    return None
예제 #2
0
def do_css_reflection(editor):
	content = editor.get_content()
	caret_pos = editor.get_caret_pos()
	css = parser_utils.extract_css_rule(content, caret_pos)
		
	if not css or caret_pos < css[0] or caret_pos > css[1]:
		# no matching CSS rule or caret outside rule bounds
		return False
		
	tokens = parser_utils.parse_css(content[css[0]:css[1]], css[0])
	token_ix = find_token_from_position(tokens, caret_pos, 'identifier')
	
	if token_ix != -1:
		cur_prop = tokens[token_ix]['content']
		value_token = find_value_token(tokens, token_ix + 1)
		base_name = get_base_css_name(cur_prop)
		re_name = re.compile('^(?:\\-\\w+\\-)?' + base_name + '$')
		re_name = get_reflected_css_name(base_name)
		values = []
			
		if not value_token:
			return False
			
		# search for all vendor-prefixed properties
		for i, token in enumerate(tokens):
			if token['type'] == 'identifier' and re.search(re_name, token['content']) and token['content'] != cur_prop:
				v = find_value_token(tokens, i + 1)
				if v:
					values.append({'name': token, 'value': v})
		
		# some editors do not provide easy way to replace multiple code 
		# fragments so we have to squash all replace operations into one
		if values:
			data = content[values[0]['value']['start']:values[-1]['value']['end']]
			offset = values[0]['value']['start']
			value = value_token['content']
				
			values.reverse()
			for v in values:
				rv = get_reflected_value(cur_prop, value, v['name']['content'], v['value']['content'])
				data = replace_substring(data, v['value']['start'] - offset, v['value']['end'] - offset, rv)
					
				# also calculate new caret position
				if v['value']['start'] < caret_pos:
					caret_pos += len(rv) - len(v['value']['content'])
				
			return {
				'data': data,
				'start': offset,
				'end': values[0]['value']['end'],
				'caret': caret_pos
			}
	
	return None
예제 #3
0
파일: token.py 프로젝트: jecelyin/sublime
def do_css_reflection(editor):
    content = editor.get_content()
    caret_pos = editor.get_caret_pos()
    css = parser_utils.extract_css_rule(content, caret_pos)

    if not css or caret_pos < css[0] or caret_pos > css[1]:
        # no matching CSS rule or caret outside rule bounds
        return False

    tokens = parser_utils.parse_css(content[css[0] : css[1]], css[0])
    token_ix = find_token_from_position(tokens, caret_pos, "identifier")

    if token_ix != -1:
        cur_prop = tokens[token_ix]["content"]
        value_token = find_value_token(tokens, token_ix + 1)
        base_name = get_base_css_name(cur_prop)
        re_name = re.compile("^(?:\\-\\w+\\-)?" + base_name + "$")
        re_name = get_reflected_css_name(base_name)
        values = []

        if not value_token:
            return False

            # search for all vendor-prefixed properties
        for i, token in enumerate(tokens):
            if token["type"] == "identifier" and re.search(re_name, token["content"]) and token["content"] != cur_prop:
                v = find_value_token(tokens, i + 1)
                if v:
                    values.append({"name": token, "value": v})

                    # some editors do not provide easy way to replace multiple code
                    # fragments so we have to squash all replace operations into one
        if values:
            data = content[values[0]["value"]["start"] : values[-1]["value"]["end"]]
            offset = values[0]["value"]["start"]
            value = value_token["content"]

            values.reverse()
            for v in values:
                rv = get_reflected_value(cur_prop, value, v["name"]["content"], v["value"]["content"])
                data = replace_substring(data, v["value"]["start"] - offset, v["value"]["end"] - offset, rv)

                # also calculate new caret position
                if v["value"]["start"] < caret_pos:
                    caret_pos += len(rv) - len(v["value"]["content"])

            return {"data": data, "start": offset, "end": values[0]["value"]["end"], "caret": caret_pos}

    return None
예제 #4
0
def update_image_size_css(editor):
    """
	Updates image size of CSS rule
 	@type editor: ZenEditor
	"""
    caret_pos = editor.get_caret_pos()
    content = editor.get_content()
    rule = parser_utils.extract_css_rule(content, caret_pos, True)

    if rule:
        css = parser_utils.parse_css(content[rule[0]:rule[1]], rule[0])
        cur_token = find_token_from_position(css, caret_pos, 'identifier')
        value = find_value_token(css, cur_token + 1)

        if not value: return False

        # find insertion point
        ins_point = find_css_insertion_point(css, cur_token)

        m = re.match(r'url\((["\']?)(.+?)\1\)', value['content'], re.I)
        if m:
            size = get_image_size_for_source(editor, m.group(2))
            if size:
                wh = {'width': None, 'height': None}
                updates = []
                styler = learn_css_style(css, cur_token)

                for i, item in enumerate(css):
                    if item['type'] == 'identifier' and item['content'] in wh:
                        wh[item['content']] = i

                def update(name, val):
                    v = None
                    if wh[name] is not None:
                        v = find_value_token(css, wh[name] + 1)

                    if v:
                        updates.append([v['start'], v['end'], '%spx' % val])
                    else:
                        updates.append([
                            ins_point['token']['end'],
                            ins_point['token']['end'],
                            styler(name, '%spx' % val)
                        ])

                update('width', size['width'])
                update('height', size['height'])

                if updates:
                    updates.sort(lambda a, b: a[0] - b[0])
                    #					updates = sorted(updates, key=lambda a: a[0])

                    # some editors do not provide easy way to replace multiple code
                    # fragments so we have to squash all replace operations into one
                    offset = updates[0][0]
                    offset_end = updates[-1][1]
                    data = content[offset:offset_end]

                    updates.reverse()
                    for u in updates:
                        data = replace_substring(data, u[0] - offset,
                                                 u[1] - offset, u[2])

                        # also calculate new caret position
                        if u[0] < caret_pos:
                            caret_pos += len(u[2]) - u[1] + u[0]

                    if ins_point['need_col']:
                        data = replace_substring(
                            data, ins_point['token']['end'] - offset,
                            ins_point['token']['end'] - offset, ';')

                    return {
                        'data': data,
                        'start': offset,
                        'end': offset_end,
                        'caret': caret_pos
                    }

    return None
예제 #5
0
파일: token.py 프로젝트: CaIIIKiH/ZenCoding
def update_image_size_css(editor):
    """
    Updates image size of CSS rule
    @type editor: ZenEditor
    """
    caret_pos = editor.get_caret_pos()
    content = editor.get_content()
    rule = parser_utils.extract_css_rule(content, caret_pos, True)
    
    if rule:
        css = parser_utils.parse_css(content[rule[0]:rule[1]], rule[0])
        cur_token = find_token_from_position(css, caret_pos, 'identifier')
        value = find_value_token(css, cur_token + 1)
            
        if not value: return False
        
        # find insertion point
        ins_point = find_css_insertion_point(css, cur_token)
        
        m = re.match(r'url\((["\']?)(.+?)\1\)', value['content'], re.I)
        if m:
            size = get_image_size_for_source(editor, m.group(2))
            if size:
                wh = {'width': None, 'height': None}
                updates = []
                styler = learn_css_style(css, cur_token)
                
                for i, item in enumerate(css):
                    if item['type'] == 'identifier' and item['content'] in wh:
                        wh[item['content']] = i
                    
                def update(name, val):
                    v = None
                    if wh[name] is not None:
                        v = find_value_token(css, wh[name] + 1)
                        
                    if v:
                        updates.append([v['start'], v['end'], '%spx' % val])
                    else:
                        updates.append([ins_point['token']['end'], ins_point['token']['end'], styler(name, '%spx' % val)])
                        
                
                update('width', size['width'])
                update('height', size['height'])
                
                if updates:
                    updates.sort(lambda a,b: a[0] - b[0])
#                   updates = sorted(updates, key=lambda a: a[0]) 
                    
                    # some editors do not provide easy way to replace multiple code 
                    # fragments so we have to squash all replace operations into one
                    offset = updates[0][0]
                    offset_end = updates[-1][1]
                    data = content[offset:offset_end]
                    
                    updates.reverse()
                    for u in updates:
                        data = replace_substring(data, u[0] - offset, u[1] - offset, u[2])
                            
                        # also calculate new caret position
                        if u[0] < caret_pos:
                            caret_pos += len(u[2]) - u[1] + u[0]
                        
                    
                    if ins_point['need_col']:
                        data = replace_substring(data, ins_point['token']['end'] - offset, ins_point['token']['end'] - offset, ';')
                    
                    return {
                        'data': data,
                        'start': offset,
                        'end': offset_end,
                        'caret': caret_pos
                    };
                    
    return None