rgbkrk / python-lsp-zed-extension

Python LSP Server Zed Extension
21 stars 1 forks source link

ruff code actions on format #3

Open ItaloCunhaBueno opened 2 months ago

ItaloCunhaBueno commented 2 months ago

I'm trying to use ruff plugin as linter and formatter on zed but the format on save is not working. Here's a part of my settings.json:

"languages": {
    "Python": {
      "show_inline_completions": false,
      "language_servers": [
        "pylsp"
      ],
      "format_on_save": {
        "language_server": {
          "name": "pylsp"
        }
      },
      "formatter": {
        "language_server": {
          "name": "pylsp"
        }
      },
      "code_actions_on_format": {
        "source.fixAll.pylsp": true,
        "source.organizeImports.pylsp": true
      }
    }
  },
  "format_on_save": "on",
  "formatter": "language_server",
  "lsp": {
    "pylsp": {
      "initialization_options": {
        "settings": {
          "python": {
            "pythonPath": ".venv/bin/python"
          }
        }
      },
      "settings": {
        "plugins": {
          "rope": {
            "enabled": true
          },
          "ruff": {
            "enabled": true,
            "formatEnabled": true,
            "config": "C:/Users/Italo/Documents/Repos/Geoja/pyproject.toml",
            "unsafeFixes": false,
            "lineLength": 256,
            "exclude": [
              "__about__.py"
            ],
            "select": [
              "ALL"
            ],
            "preview": false,
            "targetVersion": "py312"
          }
        }
      }
    }

Everything is working fine even code actions (Ctrl + .) except for the format on save part. Could someone give a little help on how to enabled pylsp to format code on save?

Thanks.

rterbush commented 2 months ago

I followed this documentation on the ruff site and ruff seems to be working. But unable to get any of the pylsp features working for some reason.

https://docs.astral.sh/ruff/editors/setup/#zed

I have the following config:

"lsp": {
    "ruff": {
      "initialization_options": {
        "settings": {
          // Ruff server settings goes here
          "lineLength": 95,
          "lint": {
            "extendSelect": ["I"],
          }
        }
      }
    },
    "pylsp": {
      "initialization_options": {
        "settings": {
          "python": {
            "pythonPath": ".venv/bin/python"
          }
        }
      },
      "settings": {
        "plugins": {
          "pylsp_mypy": {
            "live_mode": true,
            "enabled": true,
            "dmypy": true
          },
          "pylint": {
            "enabled": false
          },
          "pycodestyle": {
            "enabled": false
          },
          "jedi_symbols": {
            "enabled": true
          },
          "jedi_definition": {
            "enabled": true
          },
          "jedi_references": {
            "enabled": true
          },
          "jedi_hover": {
            "enabled": true
          },
          "jedi_completion": {
            "enabled": true
          },
          "jedi_signature_help": {
            "enablded": true
          },
          "jedi": {
            "environment": {
              "PYTHONPATH": ".venv"
            }
          }
        }
      }
    }
  },
  "languages": {
    "Python": {
      "language_servers": ["pylsp", "ruff"],
      "format_on_save": "on",
      "formatter": [
        {
          "code_actions": {
            "source.organizeImports.ruff": true,
            "source.fixAll.ruff": true
          }
        },
        {
          "language_server": {
            "name": "ruff"
          }
        }
      ],
      "python": {
        "pythonPath": ".venv/bin/python"
      },
    }
  },
rterbush commented 2 months ago

After a bit more head banging.. I seem to have this all working after removing the following section. Tried several things including attempt to set VIRTUAL_ENV=".venv" but nothing worked until removing that section and setting VIRTUAL_ENV globally in my shell.

"jedi": {
   "environment": {
        "PYTHONPATH": ".venv"
    }
 }
ItaloCunhaBueno commented 2 months ago

The standalone extension "ruff" works fine, I was trying to use the ruff as a plugin for pylsp.

rterbush commented 2 months ago

Understood..

Looking at the github for the language server, it does say that the python-lsp-ruff plugin must be installed for that functionality.

ItaloCunhaBueno commented 2 months ago

Yeah, it is installed. Well, I decided to use both ruff and pylsp extensions side by side, instead of trying to use just pylsp. Thank's for your time.