From ca68f73a3686de6f6c7f7bf9f74ec944480809be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Gonz=C3=A1lez?= Date: Wed, 16 Mar 2022 21:53:33 -0500 Subject: [PATCH] Much better support for CSS, tested using varied examples from spec --- rc/filetype/css.kak | 155 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 135 insertions(+), 20 deletions(-) diff --git a/rc/filetype/css.kak b/rc/filetype/css.kak index 7f6f7866..568c14f5 100644 --- a/rc/filetype/css.kak +++ b/rc/filetype/css.kak @@ -15,6 +15,7 @@ hook global WinSetOption filetype=css %[ require-module css hook window ModeChange pop:insert:.* -group css-trim-indent css-trim-indent + hook window InsertChar \n -group css-insert css-insert-on-new-line hook window InsertChar \n -group css-indent css-indent-on-new-line hook window InsertChar \} -group css-indent css-indent-on-closing-curly-brace set-option buffer extra_word_chars '_' '-' @@ -32,28 +33,94 @@ provide-module css %[ # Highlighters # ‾‾‾‾‾‾‾‾‾‾‾‾ - add-highlighter shared/css regions -add-highlighter shared/css/selector default-region group -add-highlighter shared/css/declaration region [{] [}] regions -add-highlighter shared/css/comment region /[*] [*]/ fill comment +add-highlighter shared/css/code default-region group +add-highlighter shared/css/attr_selector region \[ \] regions +add-highlighter shared/css/double_string region '"' (? below + # includes elements that cannot be styled, which is fine. + # + # https://developer.mozilla.org/en-US/docs/Web/HTML/Element + # + # // selector might change on site updates + # var els; document.querySelectorAll('tr td:first-child a[href^="/en-US/docs/Web/HTML/Element/"]').forEach((el) => {els += el.childNodes[0].innerText + " ";}); console.log(els); + # + html_tags='html body address article aside footer header h1 h2 h3 h4 h5 h6 main nav section blockquote dd div dl dt figcaption figure hr li ol p pre ul a abbr b bdi bdo br cite code data dfn em i kbd mark q rp rt ruby s samp small span strong sub sup time u var wbr area audio img map track video embed iframe object param picture portal source canvas noscript script del ins caption col colgroup table tbody td tfoot th thead tr button datalist fieldset form input label legend meter optgroup option output progress select textarea details dialog menu summary slot template acronym applet basefont bgsound big blink center content dir font frame frameset hgroup image keygen marquee menuitem nobr noembed noframes plaintext rb rtc shadow spacer strike tt xmp' -# https://developer.mozilla.org/en-US/docs/Web/CSS/length -add-highlighter shared/css/declaration/base/ regex (#[0-9A-Fa-f]+)|((\d*\.)?\d+(ch|cm|em|ex|mm|pc|pt|px|rem|vh|vmax|vmin|vw)) 0:value + # Units + # ‾‾‾‾‾ + # generated from the URL & below + # includes #rgb, #rrggbb, #rrggbbaa as color values {3,8} + # + # https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Values_and_Units + # + # // selector might change on site updates + # var units; document.querySelectorAll('tr td:first-child').forEach((el) => {els += el.childNodes[0].innerText + " ";}); console.log(units) + # + units='% cap ch cm deg dpcm dpi dppx em ex grad Hz ic in kHz lh mm ms pc pt px Q rad rem rlh s turn vb vh vi vmax vmin vw x' -add-highlighter shared/css/declaration/base/ regex ([A-Za-z][A-Za-z0-9_-]*)\h*: 1:keyword -add-highlighter shared/css/declaration/base/ regex :(before|after) 0:attribute -add-highlighter shared/css/declaration/base/ regex !important 0:keyword + logical_ops='and not only from to' + keywords='!important auto inherit initial unset none' + media_types='all print screen speech' -# element#id element.class -# universal selector -add-highlighter shared/css/selector/ regex [A-Za-z][A-Za-z0-9_-]* 0:keyword -add-highlighter shared/css/selector/ regex [*]|[#.][A-Za-z][A-Za-z0-9_-]* 0:variable + # easing_re='linear|ease(-(in-out|in|out))?|step-(start|end)' + + # combinators='+ > ~ ||' + # attribute_op='= ~= |= ^= $= *=' + + join() { eval set -- $1; IFS="|"; echo "$*"; } + + # Selectors + # ‾‾‾‾‾‾‾‾‾ + # universal: *, ns|*, *|*, |* + # class/id: .class, #id + # type: element + # attr: [attr=val] + + # order below matters + printf %s " + # html tag selectors + add-highlighter shared/css/code/ regex \b($(join "${html_tags}"))((:[a-z:])|[\h.#]) 1:keyword + + #functional notation + add-highlighter shared/css/code/ regex ([a-zA-Z0-9-_]+[a-zA-Z0-9])\( 1:keyword + + # logical operators, and other keywords + add-highlighter shared/css/code/ regex (\b($(join "${logical_ops}"))\b|$(join "${keywords}")) 1:keyword 1:+i + + # media types + add-highlighter shared/css/code/ regex \b($(join "${media_types}"))\b 1:+i + + # pseudo (after functional notation as they may contain paranthesis) + add-highlighter shared/css/code/ regex (:{1,2})([a-z-]+) 2:attribute 2:+a + + # at-rules + add-highlighter shared/css/code/ regex @[a-z-]+ 0:function + + # css property + add-highlighter shared/css/code/ regex ([A-Za-z][A-Za-z0-9_-]*)\h*:\h 1:operator 1:+a + + # universal, class, id selectors + add-highlighter shared/css/code/ regex (\*|[*]?[.][A-Za-z][A-Za-z0-9_-]+) 1:type + add-highlighter shared/css/code/ regex (\*|[*]?[#][A-Za-z][A-Za-z0-9_-]+) 1:type 1:+i + + # hex values + add-highlighter shared/css/code/ regex (#[0-9A-Fa-f]{3,8})\b 0:value 0:+a + add-highlighter shared/css/code/ regex \b(\d*\.)?\d+($(join "${units}"))?\b 0:value 0:+a + " +} + +# Attribute Selectors +add-highlighter shared/css/attr_selector/base default-region group +add-highlighter shared/css/attr_selector/base/ regex ([a-zA-Z0-9-]+) 1:attribute +add-highlighter shared/css/attr_selector/base/ regex \h(i|s) 1:type +add-highlighter shared/css/attr_selector/double_string region '"' (? K ] + execute-keys + try %< + # if previous line is part of a comment, do nothing + execute-keys -draft /\* ^\h*[^/*\h] + > catch %< + # else if previous line closed a paren (possibly followed by words and a comment), + # copy indent of the opening paren line + execute-keys -draft k 1s(\))(\h+\w+)*\h*(\;\h*)?(?://[^\n]+)?\n\z mJ 1 + > catch %< + # else indent new lines with the same level as the previous one + execute-keys -draft K + > # filter previous line - try %[ execute-keys -draft k : css-trim-indent ] + try %< execute-keys -draft k ^\h+$ Hd > # indent after lines ending with with { try %[ execute-keys -draft k \{$ j ] # deindent closing brace when after cursor @@ -76,6 +153,44 @@ define-command -hidden css-indent-on-new-line %[ ] ] +define-command -hidden css-insert-on-new-line %[ + evaluate-commands -draft -itersel %< + execute-keys + try %[ + # if the previous line isn't within a comment scope, break + execute-keys -draft k ^(\h*/\*|\h+\*(?!/)) + + # find comment opening, validate it was not closed, and check its using star prefixes + execute-keys -draft /\* \*/ \A\h*/\*([^\n]*\n\h*\*)*[^\n]*\n\h*.\z + + try %[ + # if the previous line is opening the comment, insert star preceeded by space + execute-keys -draft k^\h*/\* + execute-keys -draft i* + ] catch %[ + try %[ + # if the next line is a comment line insert a star + execute-keys -draft j^\h+\* + execute-keys -draft i* + ] catch %[ + try %[ + # if the previous line is an empty comment line, close the comment scope + execute-keys -draft k^\h+\*\h+$ 1s\*(\h*)c/ + ] catch %[ + # if the previous line is a non-empty comment line, add a star + execute-keys -draft i* + ] + ] + ] + + # trim trailing whitespace on the previous line + try %[ execute-keys -draft s\h+$ d ] + # align the new star with the previous one + execute-keys K1s^[^*]*(\*)& + ] + > +] + define-command -hidden css-indent-on-closing-curly-brace %[ evaluate-commands -draft -itersel %[ # align to opening curly brace when alone on a line