|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This is a VS Code extension that provides CSS variable autocomplete, color preview, and go-to-definition functionality. The extension scans CSS/SCSS/SASS/LESS files in the workspace and provides intelligent suggestions for CSS custom properties (variables starting with `--`). |
| 8 | + |
| 9 | +## Monorepo Structure |
| 10 | + |
| 11 | +This is a Turborepo monorepo with two packages: |
| 12 | + |
| 13 | +- **`packages/vscode-css-variables`**: The VS Code extension client that activates the language server and handles VS Code integration |
| 14 | +- **`packages/css-variables-language-server`**: The Language Server Protocol (LSP) implementation that powers the extension's core functionality |
| 15 | + |
| 16 | +## Common Commands |
| 17 | + |
| 18 | +### Build |
| 19 | +```bash |
| 20 | +npm run build # Build all packages using Turbo |
| 21 | +``` |
| 22 | + |
| 23 | +### Development |
| 24 | +```bash |
| 25 | +npm run dev # Watch mode for all packages |
| 26 | +``` |
| 27 | + |
| 28 | +### Testing |
| 29 | +```bash |
| 30 | +npm run test # Run tests for all packages |
| 31 | +npm run lint # Lint all packages |
| 32 | +``` |
| 33 | + |
| 34 | +### Testing Single Package |
| 35 | +```bash |
| 36 | +cd packages/vscode-css-variables && npm test # Test extension only |
| 37 | +cd packages/css-variables-language-server && npm test # Test language server only |
| 38 | +``` |
| 39 | + |
| 40 | +### Package & Deploy |
| 41 | +```bash |
| 42 | +npm run package # Package the extension (creates .vsix file) |
| 43 | +npm run deploy # Deploy to VS Code marketplace |
| 44 | +npm run release # Build and publish to npm (uses changesets) |
| 45 | +``` |
| 46 | + |
| 47 | +## Architecture |
| 48 | + |
| 49 | +### Language Server Pattern |
| 50 | + |
| 51 | +The extension follows the Language Server Protocol architecture: |
| 52 | + |
| 53 | +1. **Client (packages/vscode-css-variables/src/index.ts)**: Activates the extension and starts the language server via IPC. Reads `cssVariables.languages` configuration to determine which file types to support. |
| 54 | + |
| 55 | +2. **Server (packages/css-variables-language-server/src/index.ts)**: The LSP server that handles: |
| 56 | + - `onCompletion`: Provides CSS variable autocomplete suggestions |
| 57 | + - `onDefinition`: Enables go-to-definition for CSS variables |
| 58 | + - `onHover`: Shows variable values on hover |
| 59 | + - `onDocumentColor`: Provides color preview/decoration |
| 60 | + - File watching: Monitors CSS/SCSS/SASS/LESS files for changes |
| 61 | + |
| 62 | +### CSS Variable Management |
| 63 | + |
| 64 | +**CSSVariableManager** (packages/css-variables-language-server/src/CSSVariableManager.ts) is the core class that: |
| 65 | + |
| 66 | +- Scans workspace files based on `cssVariables.lookupFiles` glob patterns |
| 67 | +- Parses CSS/SCSS/SASS/LESS files using PostCSS with appropriate syntax parsers |
| 68 | +- Extracts CSS custom properties (declarations starting with `--`) |
| 69 | +- Supports `@import` statements with absolute URLs (fetches remote CSS files) |
| 70 | +- Detects color values using the `culori` library |
| 71 | +- Stores variables in a cache organized by file path |
| 72 | + |
| 73 | +### Cache Management |
| 74 | + |
| 75 | +**CacheManager** (packages/css-variables-language-server/src/CacheManager.ts): |
| 76 | + |
| 77 | +- Maintains two-level cache: per-file and global (all variables) |
| 78 | +- When a file changes, clears only that file's cache and removes those variables from the global cache |
| 79 | +- Supports incremental updates when files are added/modified/deleted |
| 80 | + |
| 81 | +### CSS Parsing Strategy |
| 82 | + |
| 83 | +The language server uses PostCSS with syntax-specific parsers: |
| 84 | +- `.less` files → `postcss-less` |
| 85 | +- `.scss` files → `postcss-scss` |
| 86 | +- `.css` files → standard `postcss` |
| 87 | + |
| 88 | +This allows parsing CSS variables from preprocessor files without requiring compilation. |
| 89 | + |
| 90 | +## Configuration |
| 91 | + |
| 92 | +Users can customize the extension via VS Code settings: |
| 93 | + |
| 94 | +- `cssVariables.lookupFiles`: Glob patterns for files to scan (default: CSS/SCSS/SASS/LESS) |
| 95 | +- `cssVariables.blacklistFolders`: Folders to ignore (default: node_modules, dist, etc.) |
| 96 | +- `cssVariables.languages`: File types where autocomplete is active |
| 97 | +- `cssVariables.trace.server`: LSP communication logging level |
| 98 | + |
| 99 | +## Development Notes |
| 100 | + |
| 101 | +### Working with the Extension |
| 102 | + |
| 103 | +When debugging the extension: |
| 104 | +1. Run `npm run dev` to start watch mode |
| 105 | +2. Press F5 in VS Code to launch Extension Development Host |
| 106 | +3. The language server runs on port 6009 in debug mode (see packages/vscode-css-variables/src/index.ts:24) |
| 107 | + |
| 108 | +### Testing |
| 109 | + |
| 110 | +- Extension tests are in `packages/vscode-css-variables/src/test/` and use the VS Code test framework |
| 111 | +- Language server tests are in `packages/css-variables-language-server/src/tests/` and use Jest |
| 112 | + |
| 113 | +### Adding New Features |
| 114 | + |
| 115 | +When adding LSP capabilities: |
| 116 | +1. Add the capability to `InitializeResult` in packages/css-variables-language-server/src/index.ts:57-68 |
| 117 | +2. Implement the corresponding `connection.on*` handler |
| 118 | +3. Ensure CSSVariableManager provides the necessary data |
| 119 | +4. Update configuration schema in packages/vscode-css-variables/package.json if settings are needed |
| 120 | + |
| 121 | +### Remote CSS Support |
| 122 | + |
| 123 | +The extension supports `@import` statements with HTTP/HTTPS URLs. When parsing CSS files, it: |
| 124 | +1. Walks AST to find `@import` at-rules with URL schemes |
| 125 | +2. Fetches remote CSS via axios |
| 126 | +3. Recursively parses imported CSS for variables |
| 127 | +4. Caches remote variables with the URL as the file path |
0 commit comments