Skip to main content

LSP server for Collagraph (.cgx) files with ruff integration

Project description

Collagraph LSP Server

A Language Server Protocol (LSP) implementation for Collagraph .cgx files with integrated ruff linting.

Collagraph is a Python port of Vue.js, supporting single-file components in .cgx files. This LSP server provides real-time linting and formatting for Python code within these files.

Features

  • Linting with ruff: Uses ruff to lint the Python code within the script tag
  • Formatting with ruff: Uses ruff to format the python code within the script tag and template expressions

Installation

Install from PyPi:

uv tool install collagraph-lsp
# or with pip
pip install uv

Install from source

# Clone the repository
git clone https://github.com/fork-tongue/collagraph-lsp.git
cd collagraph-lsp

# Install with uv
uv tool install .

Usage

Running the Server

The LSP server communicates over stdin/stdout. To start it:

# Using uv
uv run collagraph-lsp

# Or run directly
uv run python -m collagraph_lsp.server

Configuration

The server works with default Ruff settings. It should pick up on your configuration in your project root:

# pyproject.toml
[tool.ruff]
line-length = 120
select = ["E", "F", "W"]
ignore = ["E501"]

Editor Integration

Sublime Text

  1. Install LSP package via Package Control

  2. Manually configure LSP by adding to your settings:

{
  "clients": {
    "collagraph-lsp": {
      "enabled": true,
      "command": ["collagraph-lsp"],
      "selector": "source.collagraph | text.html.collagraph",
      "schemes": ["file"],
      "languageId": "collagraph"
    }
  }
}

VS Code

Coming soon!

Zed

Coming soon!

Example CGX File

<widget>
  <label :text="message"></label>
  <button @clicked="on_click">Click Me</button>
</widget>

<script>
import collagraph as cg
# This line has an unused import
from PySide6.QtWidgets import QBoxLayout

class TestComponent(cg.Component):
    def init(self):
        self.message = "Hello from Collagraph LSP!"

    def on_click(self):
        print(self.message)
        # This line has an error - undefined variable
        undefined_variable = "test"
</script>

Development

Running Tests

# Install (development) dependencies
uv sync

# Run tests
uv run pytest

# Run tests with coverage
uv run pytest --cov=collagraph_lsp

# Lint and format
uv run ruff check --fix
uv run ruff format

How It Works

  1. Parsing: When a .cgx file is opened or modified, the Collagraph parser extracts the <script> section with its exact line range

  2. Virtual File Creation: Creates a virtual Python file where:

    • Script section lines are preserved as-is
    • Non-script lines (template, style) are replaced with # comments
    • This preserves line numbers for accurate error reporting
  3. Template Compilation: Uses Collagraph's construct_ast to compile the template into a render() method:

    • The render method references all variables used in the template
    • This allows Ruff to see that template variables are actually used
    • For example, if QBoxLayout is imported and used in a template attribute, Ruff won't report it as unused
  4. Virtual Subclass: Appends a virtual subclass with the render method to the file:

    class VirtualDirectives(Directives):
        def render(self):  # noqa
            # ... generated code that uses template variables ...
    
  5. Linting: Runs Ruff on the complete virtual Python file (with --ignore=RUF100 to skip unused noqa warnings)

  6. Mapping: Diagnostics are correctly positioned since line numbers are preserved

  7. Publishing: Sends diagnostics to the editor via LSP protocol

License

MIT License

Related Projects

  • Collagraph - Python port of Vue.js
  • Ruff - Fast Python linter
  • pygls Library for LSP implementation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

collagraph_lsp-0.1.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

collagraph_lsp-0.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file collagraph_lsp-0.1.0.tar.gz.

File metadata

  • Download URL: collagraph_lsp-0.1.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for collagraph_lsp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3103fc808025cbd850e52bee031eda436d7b3ddfe7a02b2bc81929adec2063aa
MD5 71f4899592d0c4406168225b2042452b
BLAKE2b-256 dce92d0e92af6fbe2b91aa2238637592e801d47628536ecb72c371e9864f16ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for collagraph_lsp-0.1.0.tar.gz:

Publisher: ci.yml on fork-tongue/collagraph-lsp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file collagraph_lsp-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: collagraph_lsp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for collagraph_lsp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be3b928d1e9afae9992011254c90fbc9a14dfde58804f79092ab8a737bc7f9db
MD5 7601fea248656593636f8bf376ea06c1
BLAKE2b-256 81a34adf738b9a32221f03f5c4d493ef2660baa11b94bb870b0012b06f81ccf8

See more details on using hashes here.

Provenance

The following attestation bundles were made for collagraph_lsp-0.1.0-py3-none-any.whl:

Publisher: ci.yml on fork-tongue/collagraph-lsp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page