Author Topic: [1.8RC1] Code Editor: How to set language, and how to teach it Python  (Read 4290 times)

Offline georg.viehoever

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2132
Juan,
I sucessfully integrated the PCL2.0 CodeEditor into the Python module, see screenshot. Questions:
- from the documentation it appears I can set the type of programming language using CodeEditor.SetFilePat() http://pixinsight.com/developer/pcl/doc/html/classpcl_1_1CodeEditor.html#a7810e4e56e170fd414f2cad3384d54f2 . I would like to be able to set the type of programming language without giving it a file name (actually, I never intend to save the text I am editing). How can I do that?
- How can I teach CodeEditor some Python?
Georg
Georg (6 inch Newton, unmodified Canon EOS40D+80D, unguided EQ5 mount)

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Python syntax highlighting is not implemented yet, but I want to have it working for the final 1.8.0 release.

Quote
- How can I teach CodeEditor some Python?

Easy: Helping me :) Since I really don't know Python (besides a few simple generalities), I can't implement Python syntax highlighting properly. I need help with the following items:

- A list of Python reserved words. Of course I can find this by Googling in 5 seconds, so no special help needed here, unless there is something special that I should know about reserved words in Python.

- The same for Python operators.

- The rules for Python literals of all supported types (integer numbers, real numbers, complex numbers, strings, characters, etc.)

- Since Python uses '__*__' special identifiers, I need a list of them, a description of their purposes, and possibly a list of '__' categories.

- How are functions defined syntactically. I think simple C rules are used: an identifier followed by "( ... )", but I am not sure.

- The rules for special preprocessor sentences (if they exist).

- The rules for line comments (are they the same as in C++, i.e.: // ...)

- The rules for block comments (are they the same as in C++, i.e.: /* ... */)

- How do we have to treat formal indentation, from a syntax highlighting perspective? Or just ignore it?

- Anything else that I have not thought on.

Once we have all of these items collected and well defined, I'll translate them into regular expressions and logical constructs to implement Python syntax highlighting in CodeEditor, and ".py" file suffixes will be recognized.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline georg.viehoever

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2132
Juan,

let me try to answer some of these questions:

- you can probably start using something that already exists, such as http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting or Pygments http://pygments.org/, https://bitbucket.org/birkenfeld/pygments-main/src/f6d0af39cb77/pygments/lexers/agile.py?at=default . IDLE, spyder, emacs, eclipse have all builtin syntax highlighting for Python that can give you an idea of how your implementation might look like, see screenshot. Personally, I like the IDLE style a lot.

- The official Python documentation is http://www.python.org/doc/. The language reference http://docs.python.org/2/reference/ chapter 2 explains the lexical structure http://docs.python.org/2/reference/lexical_analysis.html including comments, keywords, operators, (string) and other rules in a very concise way.

- Python gives some _* or _*_ identifiers special semantics (such as being local members only), and some of them are used for predefined functions http://docs.python.org/2/reference/datamodel.html#specialnames. Usually, they are not specially highlighted.

- Function definition http://docs.python.org/2/reference/compound_stmts.html#function-definitions. In a nutshell:
Code: [Select]
def funcName(param1,param2,...):
    code 1
    code 2

- line comments: #

- no block comments. There is something special called doc-strings that are string literals that immediately follow a function or class definition. Syntactically they are string, but they are relevant only for documention (e.g. help(object) command). They are usually highlighted as a string (which they are), not as a comment. Example:
Code: [Select]
class myClass(baseClass):
   """This is a multiline comment explaining my class

   details line1
   details line2
   """

- Indentation: This is special in Python. Instead of using {} or begin/end, code blocks are created by indentation after the ":" of certain statements such as def, class, if, while, for, ... The rules are simple: When the lines ends with ":", indent everything that belongs to the code block with a consistent number of white spaces. Go back to the previous indentation when the code block ends. This is very similar to what most people do in C, tcl, ... anyway, but without the explicit {} or begin/end. And it is mandatory to indicate a block this way, there is just no other means. I know that many people dont like it, but others say it improves readability.
  -From the syntax highlight point of view, you can ignore indentation.
  -You can support editing by automatically increasing the indent level after a ":" at the end of the line.
  -Should you want to implement source folding: Follow the block definition given above.
     
- No preprocessor  :)

- Other: Not much:
  - You may want to highlight class/function decorators http://docs.python.org/2/glossary.html#term-decorator
  - Some, but not all, tools also highlight the most basic identifiers from Pythons standard library. They are described in the Standard Library documentation http://docs.python.org/2/library/index.html, chapter 1-6.

Let me know if I can help.
Georg
« Last Edit: 2013 January 01 06:36:58 by georg.viehoever »
Georg (6 inch Newton, unmodified Canon EOS40D+80D, unguided EQ5 mount)