PHP colorizer mode

36 views
Skip to first unread message

Adrian

unread,
Jul 9, 2017, 11:02:45 AM7/9/17
to leo-e...@googlegroups.com
Hello,

I am having some trouble figuring out how the colroizer works.  I have successfully mad some changes, but the number one thing that bothers me I do not know how to fix. 

Namely, multi line strings.  For example, I am working on some regex code that works with HTML, so I have this:

The coloring will not just be one color, it will be multiple colors as if the text was not encased inquotes.  This applies to bothphp and phpsection modes.  This also applies to HEREDOC strings. Please advise on how to make one color.  Just as""" """works correctly in python mode.

 



$foo = "
  <div class='methodsynopsis dc-description'>
   <span class='type'>string</span> <span class='methodname'><strong>mb_substr</strong></span>
    ( <span class='methodparam'><span class='type'>string</span> <code class='parameter'>$str</code></span>
   , <span class='methodparam'><span class='type'>int</span> <code class='parameter'>$start</code></span>
   [, <span class='methodparam'><span class='type'>int</span> <code class='parameter'>$length</code><span class='initializer'> = NULL</span></span>
   [, <span class='methodparam'><span class='type'>string</span> <code class='parameter'>$encoding</code><span class='initializer'> = mb_internal_encoding()</span></span>
  ]] )</div>
";


Edward K. Ream

unread,
Jul 9, 2017, 11:34:16 AM7/9/17
to leo-editor
On Sun, Jul 9, 2017 at 10:02 AM, Adrian <develope...@gmail.com> wrote:
Hello,

I am having some trouble figuring out how the colroizer works.

​Thanks for this question.  I am putting out fires at present.  I'll get to this when I can.  Perhaps tomorrow.

Edward

Adrian

unread,
Jul 9, 2017, 11:38:50 AM7/9/17
to leo-editor
Thanks!!!!!!!!!

Adrian

unread,
Jul 18, 2017, 5:03:34 PM7/18/17
to leo-editor
Hi Edward,

I know this is a small thing, but did you get a chance to look at what I might be able to do to fix this?  

Edward K. Ream

unread,
Jul 19, 2017, 8:37:30 AM7/19/17
to leo-editor
On Sun, Jul 9, 2017 at 10:02 AM, Adrian <develope...@gmail.com> wrote:

​> ​
Please advise on how to make one color.  Just as""" """works correctly in python mode.

​Why not start with the python rules as your guide. From modes/python.py:

def python_rule1(colorer, s, i):
    return colorer.match_span(s, i, kind="literal2", begin="\"\"\"", end="\"\"\"",
        at_line_start=False, at_whitespace_end=False, at_word_start=False,
        delegate="",exclude_match=False,
        no_escape=False, no_line_break=False, no_word_break=False)

def python_rule2(colorer, s, i):
    return colorer.match_span(s, i, kind="literal2", begin="'''", end="'''",
        at_line_start=False, at_whitespace_end=False, at_word_start=False,
        delegate="",exclude_match=False,
        no_escape=False, no_line_break=False, no_word_break=False)

Now do the following:

- Copy the above to modes/php.py

- Change their names to unique names, php_rule_N, php_rule_N+1.

- Add traces to these new methods so you can see when they fire.

- Add them to ​the appropriate rulesDict.  This is the key.  Leo won't try to match them unless the to-be-colorized-next character matches the lead-in character in the rulesDict.  The rulesDictDict has entries for each individual rulesDict.

The lead-in character for the first pattern is a double quote. The lead-in character for the second pattern is a single quote.

- Define @color php_literal2 = red to make for debugging.

​HTH.  Please feel free to ask more questions.

Edward​

Adrian

unread,
Jul 19, 2017, 11:14:10 AM7/19/17
to leo-editor
thank you so much Edward.

The rulesDict, with the lead-in character portion is where I was going wrong.

PS - nearing completion of the PHP autocompleter, code suggest plugin.  As usual, I need to polish it, and also figure out a way to make it work even after a user updates Leo (because I added a hack to qt_events).

Edward K. Ream

unread,
Jul 19, 2017, 12:05:01 PM7/19/17
to leo-editor
On Wed, Jul 19, 2017 at 10:14 AM, Adrian <develope...@gmail.com> wrote:
thank you so much Edward.

​You're welcome.​
 

The rulesDict, with the lead-in character portion is where I was going wrong.

​Oh good.​
 

PS - nearing completion of the PHP autocompleter, code suggest plugin.  As usual, I need to polish it, and also figure out a way to make it work even after a user updates Leo (because I added a hack to qt_events).

​You might try monkey-patching Leo's qt code.  If necessary, I'll consider adding support for, say, a new keyword arg in the appropriate qt-related method in Leo.

Edward

Adrian

unread,
Jul 19, 2017, 3:57:24 PM7/19/17
to leo-editor
By 'monkey-patching' do you mean that, if I distribute the plugin, with it's own version of qt_events for the user to install on their own?


On Sunday, July 9, 2017 at 10:02:45 AM UTC-5, Adrian wrote:

Adrian

unread,
Jul 19, 2017, 3:59:51 PM7/19/17
to leo-editor
no importa,


I honestly had never heard this term until I started reading the leo group, being new to python.

 I understand now.

Thanks, Edward

On Sunday, July 9, 2017 at 10:02:45 AM UTC-5, Adrian wrote:
Reply all
Reply to author
Forward
0 new messages