Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "./node_modules/oxlint/configuration_schema.json",
"plugins": [
"typescript"
],
"rules": {
"typescript/no-explicit-any": "error",
"typescript/consistent-type-assertions": [
"error",
{
"assertionStyle": "never"
}
],
"typescript/ban-ts-comment": [
"error",
{
"ts-expect-error": true,
"ts-ignore": true,
"ts-nocheck": true,
"ts-check": false
}
],
"typescript/no-non-null-assertion": "error"
}
}
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ const editor = new EditorView({
});
```

### Shared analysis

`sqlExtension` shares analysis between its features. To share parsing with completions:

```ts
import { createSqlAnalysis, sqlCompletion, sqlExtension } from "@marimo-team/codemirror-sql";

const analysis = createSqlAnalysis();
const extensions = [
sqlExtension({ ...analysis, schema }),
sqlCompletion({ ...analysis, dialect: StandardSQL, schema }),
];
```

For a custom parser, use `createSqlAnalysis({ parser })`. Set `needsRefresh` on
`sqlExtension` when parser configuration can change without editing SQL.
Custom parsers can implement `getCacheKey(state)` to reuse analysis across cursor moves;
see [`SqlParser`](src/sql/types.ts).

### Schema-aware semantic linting

When a schema is provided (via the top-level `schema` option, the
Expand Down
24 changes: 10 additions & 14 deletions demo/custom-renderers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { columnsOf } from "../src/sql/completion-utils.js";
import type { NamespaceTooltipData } from "../src/sql/hover.js";

interface ColumnMetadata {
Expand Down Expand Up @@ -33,24 +34,21 @@ interface TableMetadata {
export type Schema = "users" | "posts" | "orders" | "customers" | "categories" | "Users_Posts";

export const tableTooltipRenderer = (data: NamespaceTooltipData) => {
// Show table name, columns, description, primary key, foreign key, index, unique, check, default, comment
const table = data.item.path.join(".");
const columns = data.item.namespace?.[table] ?? [];
const columns = (columnsOf(data.item.namespace) ?? []).map(
(column) => typeof column === "string" ? column : column.label,
);

// Enhanced table metadata (simulated for demo purposes)
const tableMetadata = getTableMetadata(table);

let tooltip = `<div style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 13px; line-height: 1.4;">`;

// Table header
tooltip += `<div style="font-weight: 600; font-size: 14px; color: #1f2937; margin-bottom: 8px; border-bottom: 1px solid #e5e7eb; padding-bottom: 4px;">📋 Table: ${table}</div>`;

// Description
if (tableMetadata.description) {
tooltip += `<div style="color: #6b7280; font-style: italic; margin-bottom: 12px;">${tableMetadata.description}</div>`;
}

// Column details in a table
if (columns.length > 0) {
tooltip += `<div style="margin-bottom: 12px;">`;
tooltip += `<div style="font-weight: 600; color: #374151; margin-bottom: 6px;">📊 Columns</div>`;
Expand Down Expand Up @@ -92,7 +90,6 @@ export const tableTooltipRenderer = (data: NamespaceTooltipData) => {
tooltip += `</tbody></table></div>`;
}

// Foreign key relationships
if (tableMetadata.foreignKeys.length > 0) {
tooltip += `<div style="margin-bottom: 12px;">`;
tooltip += `<div style="font-weight: 600; color: #374151; margin-bottom: 6px;">🔗 Foreign Keys</div>`;
Expand All @@ -112,7 +109,6 @@ export const tableTooltipRenderer = (data: NamespaceTooltipData) => {
tooltip += `</tbody></table></div>`;
}

// Indexes
if (tableMetadata.indexes.length > 0) {
tooltip += `<div style="margin-bottom: 12px;">`;
tooltip += `<div style="font-weight: 600; color: #374151; margin-bottom: 6px;">📈 Indexes</div>`;
Expand All @@ -134,7 +130,6 @@ export const tableTooltipRenderer = (data: NamespaceTooltipData) => {
tooltip += `</tbody></table></div>`;
}

// Table statistics
tooltip += `<div style="margin-top: 12px; padding-top: 8px; border-top: 1px solid #e5e7eb; color: #6b7280; font-size: 11px;">`;
tooltip += `📊 ${tableMetadata.rowCount} rows`;
tooltip += `</div>`;
Expand All @@ -144,9 +139,7 @@ export const tableTooltipRenderer = (data: NamespaceTooltipData) => {
return tooltip;
};

// Helper function to get enhanced table metadata
function getTableMetadata(tableName: string): TableMetadata {
const metadata: Record<Schema, TableMetadata> = {
const tableMetadata: Record<Schema, TableMetadata> = {
users: {
description: "User accounts and profile information",
rowCount: "1,234",
Expand Down Expand Up @@ -310,10 +303,13 @@ function getTableMetadata(tableName: string): TableMetadata {
],
indexes: [{ name: "idx_users_posts_user", columns: ["user_id"], unique: false }],
},
};
};

const tableMetadataByName = new Map(Object.entries(tableMetadata));

function getTableMetadata(tableName: string): TableMetadata {
return (
metadata[tableName] || {
tableMetadataByName.get(tableName) ?? {
description: "Table information",
rowCount: "Unknown",
columns: {},
Expand Down
7 changes: 0 additions & 7 deletions demo/data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Schema } from "./custom-renderers";

// Default SQL content for the demo
export const defaultSqlDoc = `-- Welcome to the SQL Editor Demo!
-- Try editing the queries below to see real-time validation

Expand Down Expand Up @@ -60,9 +59,7 @@ ORDER BY total_amount DESC;
`;

export const schema: Record<Schema, string[]> = {
// Users table
users: ["id", "name", "email", "active", "status", "created_at", "updated_at", "profile_id"],
// Posts table
posts: [
"id",
"title",
Expand All @@ -73,7 +70,6 @@ export const schema: Record<Schema, string[]> = {
"updated_at",
"category_id",
],
// Orders table
orders: [
"id",
"customer_id",
Expand All @@ -83,10 +79,7 @@ export const schema: Record<Schema, string[]> = {
"shipping_address",
"created_at",
],
// Customers table (additional example)
customers: ["id", "first_name", "last_name", "email", "phone", "address", "city", "country"],
// Categories table
categories: ["id", "name", "description", "parent_id"],
// Users_Posts table
Users_Posts: ["user_id", "post_id"],
};
Loading