Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
More Convenient Code folding on ipad?
-
https://en.m.wikipedia.org/wiki/Code_folding
It is good for us to read code. And mostly we use mouse to switch between [+] and [-]. But ipad is not compatible with the mouse, maybe we can use apple pencil to replace the mouce? Then code folding should work well.
I tried the code folding on pythonista right now. Well. Can I say the f word? ... -
I think it is userful. Although I have not seen any editors support it, I asked Codea, GocoEdit, and they said code folding is a good feature.
-
See https://github.com/omz/Pythonista-Issues/issues/206 for how folding would look.
There was some support for it, basically involves long-press. I played around with trying to add buttons for it, and found it awkward, which no doubt is why it was not fully implemented in the app. Maybe with a bluetooth kb.You are aware of the "bookmarks" in the title dropdown, right? You can jump to defs, class definitions, as well as TODOs, FIXMEs, and I think NOTEs.
-
@JonB No, I mean code folding. When you click [-], you will hide the long function in editor and after clicking [+], the function shows. Just like the Visual Studio Code or Notepad ++. It is comfortable with mouse. But I'm not sure about applepencil, since none of editors on ios supported code folding.
-
@JonB By the way, maybe my English is not good, so I sent the definition on wiki.
https://en.m.wikipedia.org/wiki/Code_folding -
@lpl i understooe what you meant. the editor has some built in support for folding, but you have to manally enable it, and the manually fold sections via selection and the context menu.
The effect doid not work very well on such small screens. check out the code in that thread and try it.
-
@JonB “As an editor action, you can enable the Fold context menu item:
import editor
_ev=editor._get_editor_tab().editorView()
_ev.allowsFoldingArbitraryRanges=True
_ev.foldingEnabled=True
This would accomplish Phuket's goal, sort of, but you have to manually fold each time, and once you unfold there is not an easy way to fold again other than by selecting.”
Sorry, still I don't know how to do it. I ran the code one iphone, and it didn't work. What should I do to make it? -
Once you run the code, you can select some rows, and in the context menu you will see an option to fold.
Don't do this on a file you care about.. iirc, saving does not work correctly for folded code...
-
Sorry, still I did not make it. I ran the code and selected some rows and it just shown
Copy, Select All, --> and <---. Is this context menu? -
@lpl For testing, be very careful, don't use an important script because you could destroy it.
you will see that above the lines numbers, a triangular button that allows to unfold all folded parts.
But, this button is very close to the x button of the open tab.
if, by error, you tap the x button while a part of your script is folded, you'll loose the folded lines. I have had that during my tests and I've cried.My little script sets the tap as fold enabled like @jonb said in url...
My script must be executed as a tool while another script is edited.
It also automatically folds def or class ay first level, by a quick and dirty code, only to show. The folded part receives as name the "def/class name" and sets another backgrond color, also to show ir's possible.When fold is enabled, if yo select a text, you'll see in the popup menu the "fold" option, try it and the folded part rceives a numeric (random?) label.
If you try to run your script with folded parts, you'll receive errors because the invisible code is not taken in account.
This is really not operational but it is interesting to see what already exists in Pythonista.
import editor from objc_util import * import ui win = ObjCClass('UIApplication').sharedApplication().keyWindow() main_view = win.rootViewController().view() UIColor = ObjCClass('UIColor') # comment @on_main_thread def analysis_tree(view): sv = view.subviews() for v in sv: #print(v._get_objc_classname()) if v._get_objc_classname().startswith(b'UIButtonLabel'): vv = v.superview() if vv._get_objc_classname().startswith(b'OMCodeEditorFoldIndicatorView'): #print(v.text()) bgc = UIColor.colorWithRed_green_blue_alpha_(0.8,0.8,0.0,0.5) v.setBackgroundColor_(bgc) analysis_tree(v) @on_main_thread def main(): ev = editor._get_editor_tab().editorView() ev.allowsFoldingArbitraryRanges = True ev.foldingEnabled = True tab_path = editor.get_path() fil = open(tab_path,mode='rt',encoding='utf-8') tab_txt = fil.read() fil.close() tab_lines = tab_txt.split('\n') fold = False p = 0 for tab_line in tab_lines: #print(tab_line) if len(tab_line) > 0: if tab_line[0] not in [' ','\t']: words = tab_line.replace('(',' ').split() if not fold: if words[0].lower() in ['def','class']: fold = True fold_began = p label = words[0] + ' ' + words[1] else: fold = False start = fold_began leng = p - fold_began - 1 #print(label,start,leng) ev.foldRange_animated_userInfo_(NSRange(start,leng),True,{'label':label}) p = p - leng + 1 # because folded text disappears p = p + len(tab_line) + 1 analysis_tree(ev) main()
-
@cvp Thank you! I made it and yes, I lost my code. :D