Examples¶
Basic Server Usage¶
Starting the Server¶
from cdl_lsp import create_server
# Create and start server
server = create_server()
server.start_io()
TCP Mode¶
LSP Features in Action¶
Diagnostics¶
The server provides real-time error detection:
Valid CDL:
No diagnostics reported.Invalid System:
Diagnostic: "Unknown crystal system 'invalid'"Invalid Point Group:
Diagnostic: "Invalid point group 'xyz' for system 'cubic'"Invalid Miller Index:
Diagnostic: "Invalid Miller index notation"Auto-Completion¶
Crystal Systems¶
When typing at the system position, completions include:
cubic- Cubic crystal systemtetragonal- Tetragonal crystal systemorthorhombic- Orthorhombic crystal systemhexagonal- Hexagonal crystal systemtrigonal- Trigonal crystal systemmonoclinic- Monoclinic crystal systemtriclinic- Triclinic crystal system
Point Groups¶
After typing cubic[, completions show valid point groups:
m3m- Full cubic symmetry (48 operations)432- Cubic rotations only-43m- Cubic with mirror planesm-3- Cubic with center of symmetry23- Minimal cubic symmetry
Named Forms¶
After {, completions include named forms:
octahedron→ {111}cube→ {100}dodecahedron→ {110}trapezohedron→ {211}prism→ {10-10}basal→ {0001}
Twin Laws¶
After twin(, completions show available laws:
spinel- Spinel law (111) contact twinbrazil- Brazil law quartz twinjapan- Japan law quartz twinfluorite- Fluorite interpenetration twiniron_cross- Iron cross pyrite twin
Hover Information¶
Crystal System Hover¶
Hovering over cubic:
**Cubic Crystal System**
The highest-symmetry crystal system with a = b = c and α = β = γ = 90°.
**Point Groups:** m3m, 432, -43m, m-3, 23
**Examples:** Diamond, Garnet, Fluorite, Pyrite
Point Group Hover¶
Hovering over m3m:
**Point Group m3m**
Full cubic symmetry with 48 symmetry operations.
Also known as: Oh, 4/m -3 2/m
Includes: 3 four-fold axes, 4 three-fold axes, 6 two-fold axes,
9 mirror planes, center of symmetry
Miller Index Hover¶
Hovering over {111}:
**Miller Index {111}**
Octahedral face - all three indices equal.
In cubic system: 8 equivalent faces forming an octahedron.
**Named form:** octahedron
Code Actions¶
Quick Fix for Common Errors¶
Misspelled system:
Code action: "Did you mean 'cubic'?" → Replaces withcubic
Wrong point group:
Code action: "6/mmm is not valid for cubic. Use m3m?" → Replaces withm3m
Document Formatting¶
Before formatting:
After formatting:
Document Symbols¶
For a file containing:
Document symbols: - Line 1: Crystal (cubic/m3m) - Form: {111} - Form: {100} - Line 2: Crystal (trigonal/-3m) - Form: {10-10} - Form: {10-11}
Editor Integration Examples¶
VS Code Extension¶
Create a basic VS Code extension for CDL:
package.json:
{
"name": "cdl-vscode",
"contributes": {
"languages": [{
"id": "cdl",
"extensions": [".cdl"],
"configuration": "./language-configuration.json"
}],
"configuration": {
"cdl.server.path": {
"type": "string",
"default": "cdl-lsp"
}
}
}
}
extension.ts:
import * as vscode from 'vscode';
import { LanguageClient } from 'vscode-languageclient/node';
export function activate(context: vscode.ExtensionContext) {
const serverPath = vscode.workspace.getConfiguration('cdl').get('server.path', 'cdl-lsp');
const client = new LanguageClient(
'cdl',
'CDL Language Server',
{ command: serverPath },
{ documentSelector: [{ scheme: 'file', language: 'cdl' }] }
);
client.start();
}
Neovim Configuration¶
-- In ~/.config/nvim/lua/lspconfig/cdl_lsp.lua
local util = require('lspconfig.util')
return {
default_config = {
cmd = { 'cdl-lsp' },
filetypes = { 'cdl' },
root_dir = util.find_git_ancestor,
single_file_support = true,
settings = {
cdl = {
validation = { strict = false },
preview = { enabled = true }
}
}
},
docs = {
description = [[
CDL Language Server for Crystal Description Language
]]
}
}
Sublime Text Setup¶
CDL.sublime-syntax:
%YAML 1.2
---
name: CDL
file_extensions: [cdl]
scope: source.cdl
contexts:
main:
- match: '\b(cubic|tetragonal|orthorhombic|hexagonal|trigonal|monoclinic|triclinic)\b'
scope: keyword.other.system.cdl
- match: '\[[^\]]+\]'
scope: entity.name.tag.pointgroup.cdl
- match: '\{[^\}]+\}'
scope: string.other.miller.cdl
Preview Integration¶
Live Preview with crystal-renderer¶
from cdl_lsp import create_server
from crystal_renderer import generate_cdl_svg
import tempfile
import os
server = create_server()
@server.feature('cdl/preview')
def preview_handler(params):
cdl_string = params['cdl']
# Generate SVG preview
with tempfile.NamedTemporaryFile(suffix='.svg', delete=False) as f:
generate_cdl_svg(cdl_string, f.name)
return {'preview_path': f.name}
server.start_io()
WebView Preview¶
For VS Code with webview:
// Show preview in webview
function showPreview(svgPath: string) {
const panel = vscode.window.createWebviewPanel(
'cdlPreview',
'CDL Preview',
vscode.ViewColumn.Beside,
{}
);
const svgContent = fs.readFileSync(svgPath, 'utf8');
panel.webview.html = `
<!DOCTYPE html>
<html>
<body style="background: white; display: flex; justify-content: center;">
${svgContent}
</body>
</html>
`;
}