Uploaded image for project: 'Qt Creator'
  1. Qt Creator
  2. QTCREATORBUG-32994

Python indentation broken after using entire-file-affecting functions of pylsp (python-lsp-server)

    XMLWordPrintable

Details

    • Linux/X11

    Description

      Steps to reproduce:

      1. Preapare by creatin simple python file having unneded spaces  with 2 functions not having return statements:
        def f1():
            print(1)
        
        def f2(  ):
            print(2) 

        save it to disk outside QtCreator as q.py

      1. Launch deb-installed- QtCreator as 
      /opt/qt-creator/bin/qtcreator -temporarycleansettings q.py

      file opens

      1. Select python 3.12 in the upper dropdown (this step may be not needed, my machine has python2.7 installed and it is selected by default)
      2. Press "install" in the python language server installation suggestion
        (install succeeded)
      3. Select all via Ctrl+A
      4. Select manu "edit->advanced->autoformat selction"

      Expected result: extr spaces disappear, both function stay toplevel
      Actual result: the bottom function becomes indented as an inner function

       

      def f1():
          print(1)
      
      
          def f2():
              print(2) 

       

      I'm attaching the log from "inspect language clients" window -bad-formatting-pylsp.log 

      Insights

      According to the log it seems that the server done formatting right in the textDocument/rangeFormatting reply, but AFTER getting its result the QtCreator makes unexpected formatting breakage. As if it just inserts formatted result applying own qt creators autoindentation to it - since changing the print(1) to return - change sthe behaviour.

      Here is corresponding log exerpt, not that first broken indentation is from client, not from server

      13:19:30.575 Client
      {
          "id": "{dae2fb0a-b909-4bd1-84e1-e6a1a3fd83d1}",
          "jsonrpc": "2.0",
          "method": "textDocument/rangeFormatting",
          "params": {
              "options": {
                  "insertSpace": true,
                  "tabSize": 8
              },
              "range": {
                  "end": {
                      "character": 0,
                      "line": 5
                  },
                  "start": {
                      "character": 0,
                      "line": 0
                  }
              },
              "textDocument": {
                  "uri": "file:///home/vasiliy.galkin/q.py"
              }
          }
      }
      13:19:30.579 Server
      {
          "id": "{dae2fb0a-b909-4bd1-84e1-e6a1a3fd83d1}",
          "jsonrpc": "2.0",
          "result": [
              {
                  "newText": "def f1():\n    print(1)\n\n\ndef f2():\n    print(2)\n",
                  "range": {
                      "end": {
                          "character": 0,
                          "line": 5
                      },
                      "start": {
                          "character": 0,
                          "line": 0
                      }
                  }
              }
          ]
      }
      13:19:30.601 Client
      {
          "jsonrpc": "2.0",
          "method": "textDocument/didChange",
          "params": {
              "contentChanges": [
                  {
                      "range": {
                          "end": {
                              "character": 0,
                              "line": 5
                          },
                          "start": {
                              "character": 0,
                              "line": 0
                          }
                      },
                      "rangeLength": 47,
                      "text": "def f1():\n    print(1)\n    \n    \n    def f2():\n        print(2)\n        "
                  },
                  {
                      "range": {
                          "end": {
                              "character": 8,
                              "line": 6
                          },
                          "start": {
                              "character": 0,
                              "line": 2
                          }
                      },
                      "rangeLength": 49,
                      "text": "\n\n    def f2():\n        print(2)\n"
                  }
              ],
              "textDocument": {
                  "uri": "file:///home/vasiliy.galkin/q.py",
                  "version": 2
              }
          }
      }
      

      On formatting real files with a lot of code nearly all functions become nested into each other up to 10+ levels of nesting, making it a total mess. And same indentation problem appears after another "entire-file-affecting" refactoring "Ctrl+Shift+R" to rename symbol.

      So, large part of python LSP functionality is unusable.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            davschul David Schulz
            galkinvv vv gg
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes