omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.


    [Beta] Tinkering with Pythonista's internals using objc_util

    Pythonista
    11
    26
    19409
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • filippocld
      filippocld last edited by

      Oh, that's the most useful thing i've ever seen :O

      1 Reply Last reply Reply Quote 0
      • nfmusician
        nfmusician last edited by

        Understanding this is for experimentation only, how would one go about modifying OMPythonSyntaxHighlighter or making a custom syntax highlighter class?

        cvp 2 Replies Last reply Reply Quote 0
        • cvp
          cvp @nfmusician last edited by cvp

          @nfmusician I don't know anything about NSRegularExpression, but, only for testing, a little modif in @omz code → Code Editor Demo.py (Gist)

          allows to change rule for Python comment, from #xxxx into &xxxx

          		editor_view = OMTextEditorView.alloc().initWithFrame_syntaxHighlighterClass_theme_(f, SyntaxHighlighter, theme)
          
          		# ============= begin
          		# https://forum.omz-software.com/topic/2014/beta-tinkering-with-pythonista-s-internals-using-objc_util/12				
          		rules = editor_view.syntaxHighlighter().syntaxHighlightingRules()
          		for rule in rules:
          			print(rule.scopeName())
          			if rule.regex():
          				print('   NSRegularExpression pattern=',rule.regex().pattern())
          				if str(rule.scopeName()) == 'comment':
          					#print(dir(rule))
          					comment_pattern = rule.regex().pattern()
          					comment_pattern = ns(str(comment_pattern).replace('#','&'))
          					comment_options = rule.regex().options()
          					regex_new = ObjCClass('NSRegularExpression').alloc().initWithPattern_options_error_(comment_pattern, comment_options, None)
          					rule.setRegex_(regex_new)
          					print('   NSRegularExpression pattern=',rule.regex().pattern())
          		print(dir(rule))
          		# ============= end 
          

          1 Reply Last reply Reply Quote 1
          • cvp
            cvp @nfmusician last edited by

            @nfmusician still only for testing: how to modify color of a rule, here the comment

            		PA2UITheme = ObjCClass('PA2UITheme')
            		theme_dict = PA2UITheme.sharedTheme().themeDict().mutableCopy()
            		theme_dict.autorelease()
            		theme_dict['font-family'] = 'Menlo-Regular'
            		theme_dict['font-size'] = 14
            		# ============= begin
            		#print(theme_dict)
            		new_theme_dict = {}
            		for x in theme_dict.allKeys():
            			#print(x._get_objc_classname(),x)
            			if str(x) == 'scopes':
            				new_dict_scopes = {}
            				for y in theme_dict[x].allKeys():
            					#print(' ',y._get_objc_classname(),y)
            					if str(y) == 'comment':
            						new_dict_comment = {}
            						for z in theme_dict[x][y].allKeys():
            							#print(' ','-',z._get_objc_classname(),z)
            							if str(z) == 'color':
            								new_dict_comment[z] = "#DC7633"	# new color
            							else:
            								new_dict_comment[z] = theme_dict[x][y][z]
            						new_dict_scopes[y] = new_dict_comment
            					else:
            						new_dict_scopes[y] = theme_dict[x][y]
            				new_theme_dict[x] = new_dict_scopes
            			else:
            				new_theme_dict[x] = theme_dict[x]
            		theme_dict = new_theme_dict
            		#print(theme_dict)
            		# ============= end
            		theme = OMSyntaxHighlighterTheme.alloc().initWithDictionary_(theme_dict)
            		theme.autorelease() 
            

            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by ccc

              Is there a way to mark Python syntax errors like print “Hello World” in blinking red?

              cvp 2 Replies Last reply Reply Quote 0
              • cvp
                cvp @ccc last edited by

                @ccc It is only an editor, thus it does not check anything about Python rules. At least' that's what I think. For instance, it recognizes Python reserved words but that's all

                1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @ccc last edited by

                  @ccc Is that not what Analyze (pyflakes) tool does?

                  1 Reply Last reply Reply Quote 0
                  • ccc
                    ccc last edited by ccc

                    It is a difference between active and passive. The Pythonista editor is passive because I have to manually run pyflakes while VSCode is active because it updates the syntax highlighting as I type by running flake8. Given the speed of current iOS CPUs, active syntax highlighting might have acceptable performance.

                    cvp 2 Replies Last reply Reply Quote 0
                    • cvp
                      cvp @ccc last edited by

                      @ccc Ok, I understand but I wanted to say in my answer is that current Pythonista Editor does not allow to intercept at typing, it only checks content with some easy rules.

                      1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @ccc last edited by

                        @ccc Upon reflection, I think that it should be possible, via the delegate of the editor's TextView, to intercept the modifications of the source and to execute a code, like flak8, to check the syntax of the script. But it seems to be quite complex

                        1 Reply Last reply Reply Quote 0
                        • ccc
                          ccc last edited by

                          It would not need be letter-by-letter, but it would be cool if fixers like "isort ; black ; pyupgrade ----py37-plus" were autorun on the code in the editor. The trick would be for the fixers to fail gracefully if the user is in mid-thought.

                          cvp 1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @ccc last edited by

                            @ccc Agree but this kind of particular process in the Pythonista Editor should be for @omz only, don't you think so? And, sincerely, I don't have any hope for such functionality

                            1 Reply Last reply Reply Quote 1
                            • First post
                              Last post
                            Powered by NodeBB Forums | Contributors