CatpileDocs

Tools: VSCode Extension

Build a VSCode extension for CatLang syntax highlighting and LSP integration.

Extension Structure

Two extensions are available:

  • vscode-catpile/ - Lightweight (syntax highlighting only)
  • catpile-vscode/ - Full (syntax highlighting + LSP client)

Lightweight Extension

syntaxes/catlang.tmLanguage.json

json
{
  "scopeName": "source.cat",
  "fileTypes": ["cat"],
  "patterns": [
    {"include": "#comments"},
    {"include": "#strings"},
    {"include": "#keywords"},
    {"include": "#scope-vars"},
    {"include": "#actions"}
  ],
  "repository": {
    "comments": {
      "patterns": [{"match": "#.*$", "name": "comment.line.number-sign.cat"}]
    },
    "strings": {
      "patterns": [{"match": "\"[^\"]*\"", "name": "string.quoted.double.cat"}]
    },
    "keywords": {
      "match": "\\b(on|fn|if|else|repeat|repeat_forever|foreach|break|return|script)\\b",
      "name": "keyword.control.cat"
    },
    "scope-vars": {
      "match": "\\b[log]_[a-zA-Z_][a-zA-Z0-9_]*\\b",
      "name": "variable.other.scoped.cat"
    },
    "actions": {
      "match": "\\b(log|show|hide|set|wait|inc|dec|mul|div)\\b",
      "name": "support.function.cat"
    }
  }
}

package.json

json
{
  "name": "vscode-catpile",
  "displayName": "CatLang",
  "contributes": {
    "languages": [{
      "id": "cat",
      "aliases": ["CatLang", "cat"],
      "extensions": [".cat"],
      "configuration": "./language-configuration.json"
    }],
    "grammars": [{
      "language": "cat",
      "scopeName": "source.cat",
      "path": "./syntaxes/catlang.tmLanguage.json"
    }]
  }
}

language-configuration.json

json
{
  "comments": {"lineComment": "#"},
  "brackets": [["(", ")"], ["{", "}"]],
  "autoClosingPairs": [
    {"open": "(", "close": ")"},
    {"open": "\"", "close": "\""}
  ]
}

Full Extension (with LSP)

LSP Server

The LSP server in catpile/lsp.py provides:

  • Completion - action names, variable names, page paths
  • Hover - action documentation
  • Diagnostics - syntax errors
  • Definition - go to function definition
  • References - find variable usages

LSP Client (VSCode)

typescript
// extension.ts
import * as vscode from 'vscode';
import { LanguageClient, ServerOptions, TransportKind } from 'vscode-languageclient';

export function activate(context: vscode.ExtensionContext) {
  const serverOptions: ServerOptions = {
    command: 'python3',
    args: ['-m', 'catpile.lsp'],
    transport: TransportKind.stdio
  };

  const client = new LanguageClient('catpile', 'CatLang LSP', serverOptions, {
    documentSelector: [{ scheme: 'file', language: 'cat' }]
  });

  context.subscriptions.push(client.start());
}

Features Provided

FeatureImplementation
Syntax highlightingTextMate grammar (.tmLanguage.json)
Auto-completeLSP textDocument/completion
Error squigglesLSP textDocument/publishDiagnostics
Hover docsLSP textDocument/hover
Go to definitionLSP textDocument/definition
Find referencesLSP textDocument/references
FormattingLSP textDocument/formatting
Color previewLSP textDocument/documentColor