How to preserve code folding when formatting source - delphi

See also this question: How do I avoid expanding folded regions when I invoke the code formatter?
This is not a dupe, because I'm interested in solving the problem using the ToolsApi.
When I press CTRL + D the code formats nicely (mostly).
But all my folded code sections get unfolded.
Is there a way to keep these sections folded.
If not, is it possible to save the code folding info prior to formatting, so I can restore it later?
I'm thinking of writing IDE-addin using the Open Tools api.
I'm using XE7, but this problem exists in all versions that have source formatting.
Possible scenario's involve:
Record and replay code foldings (hook elide calls).
Only allow formatting to work on the current block (redefine the CTRL + D action).

What you can do is create regions and disable code folding, format code and then reenable the code folding.
To create code regions do:
{$REGION 'Optional text that appears when the code block is folded'}
// code
{$ENDREGION}
To toggle code folding option, press Ctrl+Shift K+O.
so, put your code into regions, fold what you want, press Ctrl+Shift K+O to disable the folding, format by pressing Ctrl+D then press Ctrl+Shift K+O again to re enable the folding.
When you re enable the folding, what was folded with a region is going to stay folded.
information source: http://docwiki.embarcadero.com/RADStudio/XE6/en/Using_Code_Folding
I hope this helps you.

Related

Block selection or multiple cursors in Texstudio?

The website of Texstudio advertises block selection or multiple cursor functionality, but I could not figure out which key combination to use for this feature reading their horribly cryptic user-manual.
I am familiar with how text selection with multiple cursors works in Sublime Text, for instance. Those key combinations don't work here.
Would someone be kind enough to please tell me how to do this in Texstudio? Thanks.
The key combination for block selection is Shift+Ctrl+Alt (See Sect. 2.8.1 in Manual)
Press them all together and simultaneously move the mouse or cursors to select the text.
Alternatively, Ctrl+Alt can be used to place multiple cursors without selecting, even in the same line or in non adjacent lines.
Without the mouse, you can use Ctrl+Alt and Up/Down arrows keys to place multicursors.
And for special use cases, there is Edit/Searching/Select All Matches, to create multi cursors after searching text with the search bar.
Edit/Selection/Select All Occurernces is like Edit/Searching/Select All Matches, but for the current word (the one where the cursor is currently placed) rather than the search.
In TeXstudio 3.0.0, there are new commands Edit/Selection/Also Select Next|Prev Occurrence to create multiple cursors to the next|prev occurrence of the current word.

Erlang and Elixir's colorful REPL shells

How does Learn some Erlang or IEx colorize the REPL shell? Is kjell a stable drop-in replacement?
The way this is done in LYSE is to use a javascript plugin called highlight.js, so LYSE isn't actually doing it, your browser is. There are plugins/modes for most mainstream(ish) languages available for highlight.js. If the web is what you are interested in, this is one way to do it (except for when a user can't use JS or has it turned off).
This isn't actually the shell being highlighted at all, nor is it useful anywhere outside of browsers. I've been messing around with a way to do this more generically, initially by inserting static formatting in HTML and XML documents (feed it a document, and it outputs one with Erlang syntax highlighted a certain way whenever this is detected/tagged). I don't yet have a decent project to publish for this (very low on my priority list atm), but I can point you in the direction of some solid inspiration: the source for wx:demo.
Pay particular attention to the function demo:code_area/1. There you will see how the tokenization routines are used to provide highlight hints for the source code text display area in the wx:demo application. This can provide a solid foundation to build your own source highlighting/display utility. (I think it wouldn't be impossible, considering every terminal in common use today responds correctly to ANSI color codes, to write a plugin to the shell that highlights terminal input directly -- not that there is a big clamor for this feature at the moment.)
EDIT (Prompted by a comment by Fred the Magic Wonder Dog)
On the subject of ANSI color codes, if this is what you are actually after, they are easy to implement as a prepend to any string value you are returning within a terminal. The terminal escapes them, so you won't see the characters, but will perform whatever action the code represents. There is no termination (its not like a markup tag that encloses the text) and typically no concept of "default color to go back to" (though there are a gajillion-jillion extensions to telnet and terminal modes that enable all sorts of nonsense like this).
An example of basic colorization is the telcon:greet/0 and telcon:sys_help/0 functions in the v0.1 code of ErlMUD (along with a slew of other places -- colorization in games is sort of a thing). What you see there is a pre-built list per color, but this could be represented any way that would get those values at the front of the string. (I just happened to remember the code value sequences, but didn't remember the characters that make them up; the next version of the code represents this somewhat differently.) Here is a link to a list of ANSI color codes and a discussion about colorizing the shell. Play around! Its nerdy fun, 1980's style!
Oh, I almost forgot... if you really want to go down the rabbit hole without silly little child toys like ncurses to help you, take a look at termcap.
I don't know if kjell is a stable drop-in replacement for Erl but it wouldn't be for IEx.
As far as how the colors are done; to the best of my knowledge it's done with ANSI Escape Sequences.

How to get outline view of Latex Project in Sublime text?

As I'm writing Latex more and more on Sublime Text (3), I decide to totally move on to ST from TexnicCenter. However there's one thing I miss from TexnicCenter, that's the ability to show the project in outline view (e.g chapter/subchapter name,...)
For example, writing a long file with one master main.tex file, and each chapter is written in a separate file, TXC gives me this view on the outline panel:
Can I achieve such thing in ST3? (I know about Ctrl + R but it does not help in this case where the chapters are written separately.)
LaTeXing also offers a SHIFT+ Ctrl + R ("Goto Symbol in Project").
LaTeXing actually seems to consider all structure markers such as \chapter{test), irrespective of whether they are \input{} somewhere, so it's not the real logical structure of the document ...
Here's a screen shot:
It's still not just in the command palette, and not a real sidebar of the logical structure and there are some confusing duplications ... but still.
Maybe the folks over at atom latex will get on it at some point; atom might be more suited for this kind of flexible UI.

How can I keep track of code folding in the code editor?

I am writing a plugin that marks specific lines, and will be trying to paint a highlight marker for specific lines over the code editor. To do this, I need to calculate the position onscreen of specific lines of code, ie rows in the buffer.
The Delphi code editor has some access to which lines are visible onscreen via IOTAEditView's BottomRow and TopRow properties. However, in newer IDE versions code regions and methods can be folded - that is, several lines are collapsed into one. The first step to line highlight painting is to know which lines are visible and where they are located, and to do this I may need to keep track of which parts of the editor are folded and which are not. There seem to be OTAPI methods to invoke code folding (elision) but not to know when it occurs.
However, some plugins, such as Castalia, do manage this. How can it be done?
An IDE editor control has a method, IsLineElided. Elision[*] is the IDE's internal term for a line being hidden when it is part of a collapsed region, method, or other structure. In the UI, this is called "folding", as in "code folding", but it's quite common for the internal term for something to be different to the UI term presented to the user.
This method is not publicly accessible; it's a method of the internal TEditControl class. To use it, you need to call an IDE method. Unlike a lot of IDE hacks you don't need to hook it, since you don't need to change its behaviour - just call it.
Mangled name: #Editorcontrol#TCustomEditControl#LineIsElided$qqri
with method prototype: TLineIsElidedProc = function(Self: TObject; LineNum: Integer): Boolean;
located in the coreide*.bpl file.
For example,
PFLineIsElided := GetProcAddress(CoreIDEHandle, StrIDELineIsElidedName);
You can get the core IDE BPL handle by reading loaded modules. The first parameter should be the editor window - not the ToolsAPI edit view, but the internal editor. This article shows the relationship between the editor control and IOTAEditView.
You can now ask if a line is elided (that is, is it hidden?) from your plugin like so:
if PFLineIsElided(FCodeEditor, 123) then ...
However, putting that together to see which areas are folded - or rather, since the top line of any folded region is still drawn, finding the line after which one or more lines are elided - require slightly more logic. The best way is to iterate through the lines onscreen in a view, IOTAEditView.TopRow and BottomRow. If the line after the one you're looking at is folded, but the one you're looking at isn't, then the one you're looking at is the representative line for the folded area (the line that has the +/- symbol in the gutter.)
Note that if you are painting on the code editor the difference between logical line numbers (line numbers as printed in the code gutter) and nominal line numbers (lines visible onscreen in the view) will be important for you, and code elision is what controls this. When code is folded, logical and nominal line numbers won't match: an edit view always draws nominal line numbers in order, but if there is a folded region in the middle, the logical line numbers will have gaps.
Further reading: A large article about integrating with the code editor, one section of which discusses code folding and handling line numbers. It's one of two on the topic of Delphi plugins / wizards integrating with the code editor on the Parnassus blog. Although it covers much more than folded code, if you're writing an IDE plugin that needs to handle this kind of stuff, there's a lot of useful material there. (Disclaimer: my blog.)
[*] As an aside, elision is an auto-antonym: a word that has two meanings that are opposites (the common example is 'cleave'.) One meaning of elision is omission or removal, and another meaning is joining or merging.

set highlight color of lua basic functions in scintilla control

I am using scintilla edit control in an MFC dialog based app.
I load scilexer.dll, and set the lexer to lua, but the only thing that is getting highlighted is the comments. I can also set keywords and they get highlighted:
mySciCtrl.SendMessage(SCI_SETKEYWORDS, 0, "for while end function")
However, I can not figure out how to enable highlighting of say lua basic functions like print, setmetatable, etc.
I thought that would be automatic just like the comments highlighting. Can anyone point me in the right direction?
Found it, it was pretty trivial of course just needed to look at the scintilla functions. So, if anyone else runs into this:
in your scintilla window class initilize color by using SendMessage(SCI_STYLESETFORE, SCE_LUA_WORD2, RGB(100,149,237)); for a second list of keywords.
Then in your dialog do m_ScinCtrl.SendMessage( SCI_SETKEYWORDS, 1, ( long )_T( "the words you want highlighted with the above specified color"));
SCE_LUA_WORD2 corresponds to integer value 1 in the second statement so if you want a third set of keywords highlighted differently just use SCE_LUA_WORD3 and integer value 2 in the second statement!

Resources