Last active
August 29, 2015 14:09
-
-
Save alexander-ae/9a29bf8241c356a45d61 to your computer and use it in GitHub Desktop.
Plugin para verificar la ruta de un selector css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
import re | |
import os | |
class CheckParentCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
# inicializa | |
view = self.view | |
settings = view.settings() | |
tab_size = settings.get('tab_size') | |
# texto | |
texto = view.substr(sublime.Region(0, view.size())).replace('\t', ' ' * tab_size) | |
lista_texto = re.split(r'\n', texto) | |
# linea actual | |
sel = view.sel() | |
point = view.rowcol(sel[0].begin()) | |
numero_linea = point[0] | |
check_parents(lista_texto, numero_linea, tab_size) | |
def obtiene_nivel(linea, tab_size): | |
""" Retorna el nivel de una linea """ | |
linea_ = linea.lstrip() | |
return linea.find(linea_) // tab_size | |
def obtiene_niveles(texto, tab_size): | |
""" Retorna una lista con los niveles de cada linea """ | |
l = [] | |
for linea in texto: | |
l.append(obtiene_nivel(linea, tab_size)) | |
return l | |
def check_parents(lista_texto, numero_linea, tab_size): | |
niveles = obtiene_niveles(lista_texto, tab_size) | |
nivel_actual = niveles[numero_linea] | |
# print("nivel_actual: " + str(nivel_actual)) | |
# print("numero_linea: " + str(numero_linea)) | |
lista_padres = [] | |
tmp_linea = numero_linea | |
tmp_nivel = nivel_actual | |
while tmp_nivel >= 0 and tmp_linea >= 0: | |
if (niveles[tmp_linea] < tmp_nivel) and (len(lista_texto[tmp_linea].strip()) > 0): | |
# print ("linea -> "+ str(tmp_linea) + " :: nivel -> " + str(tmp_nivel)) | |
lista_padres.append(tmp_linea) | |
tmp_nivel = tmp_nivel - 1 | |
tmp_linea = tmp_linea - 1 | |
# print('lista_padres: ' + str(lista_padres)) | |
texto_retorno = "" | |
for i in lista_padres: | |
texto_retorno = re.sub('[{}]', '', lista_texto[i].strip()) + ' ' + texto_retorno | |
# sublime.set_status('', texto_retorno) | |
# sublime.status_message(texto_retorno) | |
return texto_retorno | |
class NameStatusEventHandler(sublime_plugin.EventListener): | |
def check_filename(self, view): | |
file_name = view.file_name() | |
fileName, fileExtension = os.path.splitext(file_name) | |
return fileExtension | |
def on_selection_modified(self, view): | |
if self.check_filename(view) in ('.scss', '.sass', '.styl', '.less', '.css'): | |
settings = view.settings() | |
tab_size = settings.get('tab_size') | |
# texto | |
texto = view.substr(sublime.Region(0, view.size())).replace('\t', ' ' * tab_size) | |
lista_texto = re.split(r'\n', texto) | |
# linea actual | |
sel = view.sel() | |
point = view.rowcol(sel[0].begin()) | |
numero_linea = point[0] | |
texto_retorno = check_parents(lista_texto, numero_linea, tab_size) | |
sublime.status_message(texto_retorno) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ "keys": ["ctrl+1"], "command": "check_parent"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment