Metadata-Version: 2.4
Name: kompas3d-lsp
Version: 0.4.3
Summary: LSP server for KOMPAS-3D ksAPI
License-Expression: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: pygls>=2.1.0
Requires-Dist: lsprotocol>=2025.0.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.21; extra == "dev"
Requires-Dist: build>=1.0; extra == "dev"
Requires-Dist: twine>=5.0; extra == "dev"
Provides-Extra: pyinstaller
Requires-Dist: pyinstaller>=6.0; extra == "pyinstaller"

# KOMPAS-3D LSP

**Language Server Protocol** server for **KOMPAS-3D ksAPI**.

Auto-completion, hover docs, signature help and diagnostics for KOMPAS-3D scripting in Python, C++ and C#.

Powered by the official ksAPI documentation — 856 interfaces, 7104 methods with multi-language syntax (C++, Python pywin32, C# COM interop), 14106 enum member entries.

---

## Features

- **Auto-completion** — interfaces, methods, enum constants; syntax and examples match your file language
- **Hover** — method/interface descriptions with per-language syntax and examples
- **Signature help** — method signatures while typing arguments
- **Diagnostics** — common mistake warnings per language:
  - Python: unknown methods, wrong argument count, enum constant typos, unprotected `import win32com.client`
  - C++: `->` calls without null check
  - C#: COM calls without `try/catch`
- **Go-to-definition** — jump to local symbol definitions (Python)
- **Document symbols** — outline of classes/functions/variables (Python)
- **Semantic highlighting** — ksAPI interfaces, methods and enum constants (Python)
- **Find references** — usages of a symbol across open documents
- **Code actions** — quick fixes for protected-import and COM-guard diagnostics
- **`--check` batch mode** — validate a file from CLI or CI without starting the LSP server

---

## Installation

### Windows / Linux / macOS (via pip)

```bash
pip install kompas3d-lsp
kompas3d-lsp --help
```

After installation, the executable `kompas3d-lsp` (or `kompas3d-lsp.exe` on Windows) is in Python's `Scripts/` directory — add it to your PATH.

---

## Editor Integration

### OpenCode

Put `opencode.json` in your project root:

```json
{
  "lsp": {
    "kompas3d-lsp": {
      "command": ["kompas3d-lsp", "--stdio"],
      "extensions": [".py", ".cpp", ".h", ".cs"]
    }
  }
}
```

### VS Code

Install the [vscode-languageserver-client](https://marketplace.visualstudio.com/items?itemName=brandonfowler.vscode-languageserver-client) extension, then add this to your workspace `.vscode/settings.json`:

```json
{
  "lsp.launch.vscode-languageclient": [
    {
      "id": "kompas3d-lsp",
      "name": "KOMPAS-3D LSP",
      "command": ["kompas3d-lsp", "--stdio"],
      "languageIds": ["python", "cpp", "csharp"],
      "fileExtensions": [".py", ".pyw", ".cpp", ".cxx", ".cc", ".h", ".hpp", ".hxx", ".cs"]
    }
  ]
}
```

Restart VS Code — the server starts automatically when you open a matching file. Set `"environment"` in the same object if you need to pass env vars (e.g. `KOMPAS_BIN_DIR`).

### Neovim (vim.lsp)

```lua
vim.api.nvim_create_autocmd("FileType", {
  pattern = "python",
  callback = function()
    vim.lsp.start({
      name = "kompas3d-lsp",
      cmd = { "kompas3d-lsp", "--stdio" },
    })
  end,
})
```

---

## Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `KOMPAS_LSP_LANG` | `ru` | Knowledge base language (`ru` / `en`) |
| `KOMPAS_LSP_DATA_DIR` | — | Custom path to `{lang}/ksapi_knowledge.json` |
| `KOMPAS_BIN_DIR` | — | Path to KOMPAS-3D `Bin/` for exact method name lookup |

### `KOMPAS_BIN_DIR`

Points to the installed KOMPAS-3D build directory (`Bin/`). The server loads method names directly from `ksapi.py` and `KsAPI.h` of that build, eliminating false positives from documentation drift.

Without `KOMPAS_BIN_DIR`, the server falls back to the knowledge base with severity **hint** (not warning).

**Windows:**

```powershell
$env:KOMPAS_BIN_DIR = "C:\Program Files\ASCON\KOMPAS-3D v25\Bin"
kompas3d-lsp --check myscript.py
```

---

## CLI

```
kompas3d-lsp --stdio                  # stdio transport (default, for LSP clients)
kompas3d-lsp --tcp                    # TCP transport (127.0.0.1:2087, debugging)
kompas3d-lsp --lang en                # override knowledge base language
kompas3d-lsp --check file.py          # batch file check (exit code 1 on findings)
kompas3d-lsp --check file.py --json   # machine-readable JSON output
kompas3d-lsp --help
```

### Exit codes

- **0** — clean file (no findings)
- **1** — issues found

Use in pre-commit hooks or CI gates:

**Windows (PowerShell):**

```powershell
kompas3d-lsp --check myscript.py --json; if ($LASTEXITCODE) { throw "ksAPI issues found" }
```

### Output format

Default (human-readable): `file:line:col: severity: message`

```
myscript.py:3:5: hint: Method 'GetBodyParts' not found in SDK documentation.
myscript.py:7:12: warning: Method 'GetGabarit' called with 1 arguments; expected 4–8.
myscript.py:9:23: hint: Unknown enum constant: 'constants3d.o3d_facce'.
```

With `--json`: array `[{line, character, message, severity, source}]`.

---

## Diagnostics

### Python (AST-based)

- Unknown ksAPI method — hint, or warning when `KOMPAS_BIN_DIR` is set
- Wrong argument count (arity), including default parameters and keyword arguments
- Enum constant typos: `constants3d.o3d_facce`
- `import win32com.client` without `try/except` — KOMPAS may be missing on CI

### C++

- `->` method call without a null check
- Unknown method (comments and string literals are ignored)

### C#

- `Marshal.GetActiveObject` / `Type.GetTypeFromProgID` without `try/catch`
- Unknown method (comments, string literals, `std::` and framework methods are ignored)

Comments and string literals never trigger diagnostics — false positives from commented-out code are excluded.

---

## Supported Languages

| Extension | Language | Syntax Style |
|-----------|----------|-------------|
| `.py`, `.pyw` | Python | `method(param: type) -> type` |
| `.cpp`, `.cxx`, `.cc`, `.h`, `.hpp`, `.hxx` | C++ | Native from documentation |
| `.cs` | C# | `Type method(params);` |

Language is auto-detected by file extension. The server shows syntax, examples and diagnostics matching the file language.

---

## Changelog

### v0.4.3
- C++/C# diagnostics ignore comments and string literals
- `--check` for C++/C# skips `std::` and framework methods; catches `->` calls
- Python enum-constant check supports import aliases (`import constants3d as c3d`)

### v0.4.2
- Find references across open workspace documents
- Configurable diagnostic severity (`didChangeConfiguration`)
- Code actions: quick fixes for `import win32com.client` (try/except) and C# COM guard (try/catch)

### v0.4.1
- Go-to-definition: local Python symbols
- Document symbols: class/function/variable outline
- Semantic highlighting: ksAPI interfaces, methods, enum constants

### v0.4.0
- Faster knowledge-base lookups
- Completion prefix gating — no large result payloads on every keystroke
- Debounced diagnostics on document change
- Robust language detection for `untitled:`, notebook cells and query/fragment URIs

### v0.3.0

- Exact method name check via `KOMPAS_BIN_DIR` (`ksapi.py` + `KsAPI.h`)
- AST diagnostics for Python: call chains, arity, enum constants
- Python out-parameters as tuple return types
- Enum member auto-completion (14106 entries)
- `--check`: exit code 1 on findings; human-readable output by default, `--json` for JSON

### v0.2.0

- Multi-language pipeline (C++ → Python/C# syntax)
- `--check` batch mode
- pygls 2.x support, lazy knowledge-base loading

---

## License

MIT.

---

## Links

- [KOMPAS-3D SDK Documentation](https://help.ascon.ru/KOMPAS_SDK/25/ru-RU/index.html)
- [LSP Specification](https://microsoft.github.io/language-server-protocol/)
- [pygls](https://github.com/openlawlibrary/pygls)
