```
██╗  ██╗███████╗    ██╗      █████╗ ███╗   ██╗ ██████╗ ██╗   ██╗ █████╗  ██████╗ ███████╗
╚██╗██╔╝██╔════╝    ██║     ██╔══██╗████╗  ██║██╔════╝ ██║   ██║██╔══██╗██╔════╝ ██╔════╝
 ╚███╔╝ ███████╗    ██║     ███████║██╔██╗ ██║██║  ███╗██║   ██║███████║██║  ███╗█████╗
 ██╔██╗ ╚════██║    ██║     ██╔══██║██║╚██╗██║██║   ██║██║   ██║██╔══██║██║   ██║██╔══╝
██╔╝ ██╗███████║    ███████╗██║  ██║██║ ╚████║╚██████╔╝╚██████╔╝██║  ██║╚██████╔╝███████╗
╚═╝  ╚═╝╚══════╝    ╚══════╝╚═╝  ╚═╝╚═╝  ╚═══╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝ ╚═════╝ ╚══════╝

███████╗███████╗██████╗ ██╗   ██╗███████╗██████╗
██╔════╝██╔════╝██╔══██╗██║   ██║██╔════╝██╔══██╗
███████╗█████╗  ██████╔╝██║   ██║█████╗  ██████╔╝
╚════██║██╔══╝  ██╔══██╗╚██╗ ██╔╝██╔══╝  ██╔══██╗
███████║███████╗██║  ██║ ╚████╔╝ ███████╗██║  ██║
╚══════╝╚══════╝╚═╝  ╚═╝  ╚═══╝  ╚══════╝╚═╝  ╚═╝
```

# XanoScript Language Server

A Language Server Protocol (LSP) implementation for [XanoScript](https://xano.com), built on [Chevrotain](https://chevrotain.io/).

Maintained by the [Xano](https://xano.com) team.

## Installation

```sh
npm install @xano/xanoscript-language-server
```

**Requirements:** Node.js 20+

## Features

The language server provides full LSP support including:

- **Code Completion** - Context-aware suggestions triggered by `.`, `:`, `$`, and `|`
- **Syntax Highlighting** - Semantic token-based highlighting for XanoScript constructs
- **Hover Information** - Documentation and type information on hover
- **Diagnostics** - Real-time error detection and reporting

## Supported Object Types

The parser supports the following XanoScript constructs:

| Type | Description |
|------|-------------|
| `query` | API endpoints with HTTP verbs, input validation, and responses |
| `function` | Reusable logic blocks with testing capabilities |
| `task` | Scheduled operations with cron-like triggers |
| `api_group` | Collections of related API endpoints |
| `table` | Database schema definitions with column types |
| `table_trigger` | Database table trigger definitions |
| `workflow_test` | Test definitions for workflows |
| `addon` | Add-on definitions |
| `agent` | AI agent definitions |
| `agent_trigger` | Agent trigger handlers |
| `branch` | Branch logic |
| `mcp_server` | MCP server definitions |
| `mcp_server_trigger` | MCP server trigger handlers |
| `middleware` | Middleware definitions |
| `realtime_channel` | Realtime channel definitions |
| `realtime_trigger` | Realtime trigger handlers |
| `tool` | Tool definitions |
| `workspace` | Workspace definitions |
| `workspace_trigger` | Workspace trigger handlers |

## Built-in Function Domains

XanoScript includes functions organized by domain:

- **ai** - AI and machine learning operations
- **api** - External API calls and HTTP requests
- **array** - Array manipulation and iteration
- **cloud** - Cloud service integrations
- **controls** - Flow control (conditionals, loops, etc.)
- **db** - Database queries and operations
- **debug** - Debugging and logging utilities
- **expect** - Testing assertions
- **math** - Mathematical operations
- **object** - Object manipulation
- **redis** - Redis cache operations
- **schema** - Schema operations
- **security** - Authentication and security functions
- **storage** - File storage operations
- **stream** - Streaming data operations
- **text** - String manipulation
- **util** - General utilities
- **var** - Variable operations
- **zip** - Compression utilities

## Project Structure

```
├── server.js              # LSP server entry point
├── lexer/                 # Lexical analysis
│   ├── lexer.js           # Chevrotain lexer implementation
│   ├── tokens.js          # Main token registry
│   └── [domain].js        # Domain-specific tokens (api, db, cloud, etc.)
├── parser/                # Syntax parsing
│   ├── base_parser.js     # Core XanoBaseParser
│   ├── [type]_parser.js   # Object type parsers
│   ├── attributes/        # Field attributes (description, disabled, sensitive)
│   ├── clauses/           # Language blocks (stack, input, response, etc.)
│   ├── definitions/       # Type and column definitions
│   ├── functions/         # Built-in functions by domain
│   └── generic/           # Reusable parsing components
├── onCompletion/          # Code completion logic
├── onDidChangeContent/    # Diagnostics and error reporting
├── onHover/               # Hover documentation
└── onSemanticCheck/       # Semantic token highlighting
```

## Development

```sh
# Install dependencies
npm install

# Run all tests (~1800 tests)
npm test

# Run specific test
npm test -- -g "test name"

# Lint code
npm run lint

# Fix lint issues
npm run lint:fix

# Generate TextMate language definition
npm run tm
```

## Adding New Features

### New Object Type

1. Create a new parser in `parser/` (e.g., `my_object_parser.js`)
2. Follow existing parser patterns (see `query_parser.js`, `function_parser.js`)
3. Register it in `server.js`

### New Keyword/Function

We have a hierarchy for the different statements XanoScript uses:

1. **Parser** - Top level (Query parser, Function parser, etc.)
2. **Clause** - Main blocks in a given object type (stack, response, input, etc.)
3. **Functions** - Statements like `debug.log` or `db.query`
4. **Attributes/Definitions** - Components used to define functions
5. **Generic** - Shared language toolset

To add a new function:

1. Add token definition in appropriate `lexer/` file
2. Create function implementation in `parser/functions/[domain]/`
3. Register in parent clause or parser
4. Add comprehensive tests in corresponding `.spec.js` file

## Testing

The language server is heavily tested and should remain that way. Tests should be:

- Easy to copy, paste, and extend
- Straightforward and easy to read
- Avoid dynamic generation (no loops)

Test files use `.spec.js` naming convention and mirror the source structure.

## CI/CD

On commit, pre-commit hooks run linting via Husky. The CI pipeline runs all tests and linting checks. Configuration is in `.github/workflows/ci.yml`.

## License

MIT License - see [LICENSE](LICENSE) for details.
