python-lsp / python-lsp-server

Fork of the python-language-server project, maintained by the Spyder IDE team and the community
MIT License
1.94k stars 194 forks source link

Insert space/colon for keywords always followed by space/colon #23

Open krassowski opened 3 years ago

krassowski commented 3 years ago

There is a number of keywords which are always followed by space or colon in Python:

KEYWORDS_FOLLOWED_BY_SPACE = {
    'as',
    'assert'
    'async',
    'await',
    'class',
    'def',
    'del',
    'for',
    'from',
    'global',
    'import',
    'in',
    'is',
    'nonlocal',
    'raise',
    'while',
    'with',
    # conditional
    'elif'
    'if',
    # logical
    'and',
    'or',
    'not',
}
# note: 'yield' and 'return' can be followed by space but can also be standalone

# keywords that are ALWAYS followed by colon
KEYWORDS_FOLLOWED_BY_COLON = {
    'try',
    'finally'
}
# note: 'else' can be used in a ternary expression: x = 1 if y else 2
# note: 'lambda' can be followed by colon or argument: lambda: 1 or lambda x: x + 1

Many editors modify their insert text to automatically add space/colon in such situations. I would propose that python-lsp-server does the same. Example implementation: https://github.com/krassowski/python-language-server/pull/5/files

Opening to get opinions and feedback.

krassowski commented 3 years ago

Three thoughts:

ccordoba12 commented 3 years ago

Sounds good to me.