Assign Language by Line
-
@Valley-Moose said:
Is there a way that I could use different language syntax highlighting within the same file
By default. No. There’s not even a builtin way to say “parse this file for both HTML and JS and CSS”.
However, theEnhanceAnyBuiltinLexer.py
that @Ekopalypse shares here, you may be able to add regular expressions which would do bonus highlighting in certain circumstances. I don’t know if that’s sufficient for you… -
I have to be honest, I’m not sure how exactly to use this. I read through the discussion and it was a bit over my head. Could you provide me with a little more explanation?
-
If the discussion is over your head, then modifying it for your need likely is as well. Maybe Eko will see value in that kind of thing and modify his script accordingly (or create a new one).
-
@Valley-Moose said:
Could you provide me with a little more explanation?
I’ve never used it; I just advocate it. :-) Hopefully, @Ekopalypse will chime in.
-
@Alan-Kilborn I’m a pretty fast learner, but discerning what is relevant to me by reading someone else’s conversation on a similar topic and understanding what I need to do… is a bit difficult.
-
I’m not 100% certain I understand what you trying to achieve.
Do you need some kind of “color this line different than the others”, or do you really need “this line should be colored like the sql lexer does as it is sql” and “this line should be colored as perl lexer does as it is really perl” etc. ? Or do you want to have certain words or text fragments colored differently?
Could you provide an example of such a text? -
@Ekopalypse please excuse my ignorance, but I believe I need: “this line should be colored like the sql lexer does as it is sql” and “this line should be colored as perl lexer does as it is really perl” etc. - If I set the language to DAX, the entire file will use DAX syntax highlighting. If I set the language to SQL, the file will use SQL syntax highlighting. I want to be able to highlight specific text and assign the syntax to use. I’m told that Sublime Text 3 allows for this, but only with related languages like java and c++ etc.- It may be that I am not asking my question correctly, but I hope this clarified it a little.
-
@Valley-Moose said:
@Ekopalypse please excuse my ignorance, but I believe I need: “this line should be colored like the sql lexer does as it is sql” and “this line should be colored as perl lexer does as it is really perl” etc. - If I set the language to DAX, the entire file will use DAX syntax highlighting.
I don’t know if it helps to know that I added DAX syntax highlighting following instructions given here - since it is not available by default:
http://sascha-kasper.com/dax-syntax-highlighting-for-notepad/ -
nothing to excuse.
I’m not a native English speaker and just wanted to be sure to understand it correctly, that’s why a asked for clarification.Having two lexers (code which does the coloring) active at the same time isn’t supported by npp/scintilla.
There is a concept of sublexers but as far as I know, the only lexer which does this is the html lexer. This colors html, javascript, php etc… in one lexer.So, in order to be able to make this work for you one would either write a lexer which
does the lexing for both languages or you have to decide to use one builtin lexer and enhance it with additional colorings like @PeterJones mentioned.As I don’t know DAX, well I know DAX as the german stock index, as a language can
you provide me a link or details about its syntax? -
Are you looking for something like this - https://docs.microsoft.com/en-us/dax/dax-syntax-reference
This may also be helpful - https://www.sqlbi.com/articles/rules-for-dax-code-formatting/
Otherwise, here is an sample of something I might write:
POSI TCs:=CALCULATE(SUM(Data[Trouble Call]),FILTER(all(Data), Data[Position] = values(Data[Position])))- POSI TCs: - is the name of the Measure
- Data - is the name of the Table
- Trouble Call & Position - are Column Names within the Table
(How do I include an example from notepad++, so you can see what it looks like for me? -I saw that you were able to do that in your posts - As you may be able to tell, I’m new to posting questions on forums.)
-
thank you, that is exactly what I was looking for.
To include code you can use 3 tildes before the code and 3 tildes afterwards.
Like~~~ code ~~~
.
To include a screenshot, you have to upload it to some hoster like imgur.com and
include it like![](https://i.imgur.com/qq9erDL.jpg)
woluld result in -
@Valley-Moose said:
I’m a pretty fast learner, but discerning what is relevant to me by reading someone else’s conversation on a similar topic and understanding what I need to do… is a bit difficult.
Understood. It would take some absorption…which means time. :)
I want to be able to highlight specific text and assign the syntax to use
And I think I know the next thing you’ll want: Notepad++ to remember (over multiple runs) your setting for text you previously did this to. It’s a bit of a challenge.
-
-
after thinking about it for some time I can imagine how this could be achieved.
In theory:- using a hidden scintilla document
- and a script for selecting which lexer should be used
workflow:
- select text
- run script and select lexer from a messagebox, prompt, or whatever ui
- now, under the hood, the script would get the text selected and paste it into the
hidden scintilla document and sets the lexer. Then it reads the styles assigned to
the text and sets it in the real document
Is this possible? I don’t know, I never done something like this but is something I’m eager to find out. So I need some time playing around and let you know.
Concerning the “Can I stop npp, restart some time later and the coloring is still there?”,
I would say, yes, if a strict policy is in place. Something like a file must be saved always and this file is not allowed to be manipulated outside from npp. -
I’ve tested the concept and it looks like it could work, BUT
there are some strange things happening.Assuming one wants to color every word of a text in red.
When using these three lines to do the coloringeditor.setIndicatorCurrent(INDICATOR_ID) editor.setIndicatorValue(color | SC_INDICVALUEBIT) editor.indicatorFillRange(start, length)
there is an extreme difference in performance if you call it directly or via a callback
like SCINTILLANOTIFICATION.UPDATEUI.If one is using a callback it takes 11ms to color 377 words,
if calling it directly, it takes 6 seconds. ???In addition, if calling it directly, it happens that not all words get colored.
Sometimes it misses 2 or 3 or … words even so the debug shows that those missed
words are actually processed. ???What haven’t I understood?
And some more strange things. Just for the fun, I’ve tested a python only version,
means building a window and embedding scintilla in pure python,
but python3 I must admit, it took 3ms to do the same thing.
What is slowing down when using PS?? -
I think there are some “slowness” gremlins in Pythonscript that no one has totally figured out yet. Here’s a reference to another type, maybe some hints here for you Eko because from observation it definitely seems like you have the skills to maybe solve it or at least root cause it. I was hoping @Claudia-Frank would solve these things, but no luck and she has disappeared. Maybe you are the new Claudia. ;)
I’ve tested a python only version, means building a window and embedding scintilla in pure python
I would like to understand how to do that, sure, just for fun. :)
-
Alan, thanks for the link but, oh lord, if they were confused how should I find it?
They have far more experience in c++ than I do. Phew.Maybe you are the new Claudia.
But I don’t have to turn myself into a woman, or? LOL :-D
I would like to understand how to do that, sure, just for fun. :)
:-) Claudia posted this and I slightly modified it.
def MainLoop(self): self.ColorEachWordRed() gui.PumpMessages() def ColorEachWordRed(self): SC_INDICVALUEBIT = 0x1000000 INDICATOR_ID = 8 TEXTFORE = 17 VALUEFORE = 1 # editor1.indicSetStyle(INDICATOR_ID, INDICATORSTYLE.TEXTFORE) self.scintilla_direct_function(self.sci_direct_pointer, 2080, INDICATOR_ID, TEXTFORE) # editor1.indicSetFlags(INDICATOR_ID, INDICFLAG.VALUEFORE) self.scintilla_direct_function(self.sci_direct_pointer, 2684, INDICATOR_ID, VALUEFORE) t1_start = time.time() i = 0 for match in re.finditer(b'\w+', self.text): i += 1 start, end = match.span() self.scintilla_direct_function(self.sci_direct_pointer, 2500, INDICATOR_ID) self.scintilla_direct_function(self.sci_direct_pointer, 2502, 255 | SC_INDICVALUEBIT) self.scintilla_direct_function(self.sci_direct_pointer, 2504, start, end-start) t1_stop = time.time() print('Colored %d items' % i) print("Elapsed time: %.6f" % (t1_stop-t1_start))
self.text, my test data, is this
self.text = b'''Hello World here is python Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed molestie nisl placerat, ultricies risus nec, aliquam nunc. Nunc urna metus, molestie ut augue vitae, posuere faucibus elit. Donec sodales est tortor, eu aliquet massa hendrerit et. Aliquam pretium fermentum volutpat. Aliquam et lorem id orci tincidunt porta. Duis vehicula, mi eu tempor cursus, mauris ex efficitur libero, ac ultricies dui enim eu dui. Proin vel diam pulvinar, consequat libero ac, interdum lorem. Nam interdum ut sapien eu viverra. Nunc molestie efficitur sollicitudin. Morbi vel fermentum velit. 123 Aliquam nec lectus a enim vulputate pretium. Nunc elementum tristique porttitor. Curabitur malesuada sed augue sed lobortis. Vivamus sit amet pharetra dolor. Etiam finibus gravida felis, sed porttitor sapien. In lobortis purus eu ipsum auctor varius. Cras eu tempor urna, quis iaculis nunc. Ut purus lectus, ultrices congue elementum quis, pellentesque vel eros. Mauris eget dolor non dolor finibus mollis vel et elit. Quisque vestibulum orci non nulla pulvinar, ac efficitur velit tempor. Cras non sem pellentesque, feugiat sapien sed, commodo elit. Cras dolor augue, eleifend vel urna vitae, consequat blandit lectus. Curabitur porttitor neque in mi ornare dictum. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Duis ante nisl, facilisis tincidunt odio quis, placerat molestie dolor. 456 Phasellus rutrum posuere convallis. Proin dictum ex eget nisl eleifend tempus. Cras id lorem fermentum, molestie quam eu, commodo odio. Curabitur id quam tellus. Suspendisse potenti. Etiam egestas tincidunt dui, quis ornare nulla rhoncus quis. Suspendisse sodales elit vel felis vestibulum mollis. 789 Aenean quis massa laoreet, sagittis est ac, bibendum risus. Praesent eu sapien faucibus, porta orci non, ultricies elit. Morbi a dapibus neque. Nunc at odio eget risus egestas efficitur. Duis ullamcorper velit diam, et volutpat lacus malesuada eget. Mauris dignissim tincidunt rutrum. Sed molestie ut nisl ac dictum. Vestibulum a libero ut turpis tristique vehicula. Nam lobortis, leo vitae pellentesque sollicitudin, dolor leo viverra enim, sit amet ullamcorper metus magna eget ipsum. Ut ac eros porta, lacinia sem ac, posuere elit. Nam at efficitur orci, nec volutpat ligula. Quisque non nisi sit amet arcu accumsan viverra quis et tellus. Fusce accumsan, nunc vel semper sollicitudin, elit ante congue velit, vitae dignissim tellus risus ac justo. Duis non leo lorem. 000 '''
Make sure your SciLexer.dll is in the same directory as your script and, of course,
that python bitness matches SciLexer.dll bitness.Edit: You need to pip install win32 package.
-
just in case you want to test the other two
direct call
from Npp import editor, notepad, INDICATORSTYLE, INDICFLAG import time SC_INDICVALUEBIT = 0x1000000 INDICATOR_ID = 8 editor1.indicSetStyle(INDICATOR_ID, INDICATORSTYLE.TEXTFORE) editor1.indicSetFlags(INDICATOR_ID, INDICFLAG.VALUEFORE) editor2.indicSetStyle(INDICATOR_ID, INDICATORSTYLE.TEXTFORE) editor2.indicSetFlags(INDICATOR_ID, INDICFLAG.VALUEFORE) def paint_it(color, start, length): editor.setIndicatorCurrent(INDICATOR_ID) editor.setIndicatorValue(color | SC_INDICVALUEBIT) editor.indicatorFillRange(start, length) s1 = time.time() matches = [] editor.research(r'\w+', lambda m: matches.append(m.span())) for match in matches: paint_it(255, match[0], match[1] - match[0]) print time.time() - s1
via callback
from Npp import editor, notepad, INDICATORSTYLE, INDICFLAG import time SC_INDICVALUEBIT = 0x1000000 INDICATOR_ID = 8 editor1.indicSetStyle(INDICATOR_ID, INDICATORSTYLE.TEXTFORE) editor1.indicSetFlags(INDICATOR_ID, INDICFLAG.VALUEFORE) editor2.indicSetStyle(INDICATOR_ID, INDICATORSTYLE.TEXTFORE) editor2.indicSetFlags(INDICATOR_ID, INDICFLAG.VALUEFORE) def paint_it(color, start, length): editor.setIndicatorCurrent(INDICATOR_ID) editor.setIndicatorValue(color | SC_INDICVALUEBIT) editor.indicatorFillRange(start, length) def on_updateui(args): s1 = time.time() matches = [] editor.research(r'\w+', lambda m: matches.append(m.span())) for match in matches: paint_it(255, match[0], match[1] - match[0]) print time.time() - s1 editor.clearCallbacks([SCINTILLANOTIFICATION.UPDATEUI]) editor.callbackSync(on_updateui, [SCINTILLANOTIFICATION.UPDATEUI])
Edit: be careful about the callback as it will color every document, use it, maybe, in its own npp instance :-)
-
@Ekopalypse said:
Claudia posted this and I slightly modified it.
Ah…well it appears that even if you are not the new Claudia, you are at least “channeling” the old Claudia. That posting from Claudia was only 4 days old…seems she is still in the Scintilla game even if not the N++ game…maybe she is making the next new great competitor for N++??
if they were confused how should I find it?
You seem to have a knack for this stuff (way beyond my basic skill); maybe all it takes is a pair of fresh eyes?
BTW, I have a new (perhaps tough?) problem for Pythonscript that I’ll be posting here (well, in a brand-new thread) soon…hopefully you’ll be watching for it. :)
-
she recently posted at pythonscript github so I’m hoping that she might find her
way back home and of course, I’m always looking for python related stuff :-D