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
2 changes: 2 additions & 0 deletions packages/schema/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/out/
/tests/coverage/
/bundle
*.vsix
8 changes: 7 additions & 1 deletion packages/schema/.vscodeignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
.vscode/**
.vscode-test/**
.gitignore
langium-quickstart.md
src
tests
node_modules
bin
build
jest.config.ts
tsconfig.json
9 changes: 9 additions & 0 deletions packages/schema/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2022 ZenStack Language Tools

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion packages/schema/bin/cli
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

require("../out/cli").default();
require('../bundle/cli').default();
41 changes: 0 additions & 41 deletions packages/schema/langium-quickstart.md

This file was deleted.

45 changes: 34 additions & 11 deletions packages/schema/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
{
"name": "zenstack",
"displayName": "ZenStack CLI and Language Tools",
"description": "ZenStack CLI and Language Tools",
"version": "0.1.47",
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "ZenStack is a toolkit that simplifies full-stack development",
"version": "0.1.49",
"author": {
"name": "ymc9"
},
"keywords": [
"fullstack",
"react",
"typescript",
"data modeling"
],
"preview": true,
"icon": "asset/logo-light.png",
"repository": {
"type": "git",
"url": "https://github.com/zenstackhq/zenstack"
},
"engines": {
"vscode": "^1.56.0"
"vscode": "^1.72.0"
},
"categories": [
"Programming Languages"
Expand Down Expand Up @@ -40,23 +56,27 @@
],
"files": [
"bin",
"out"
"src",
"bundle"
],
"bin": {
"zenstack": "./bin/cli"
},
"main": "./out/extension.js",
"main": "./bundle/extension.js",
"scripts": {
"vscode:prepublish": "npm run build && npm run lint",
"build": "npm run langium:generate && tsc && tsc-alias && cp src/language-server/stdlib.zmodel ./out/language-server/ && cp src/generator/*.template.json ./out/generator/",
"ts:watch": "tsc --watch",
"vscode:publish": "vsce publish --no-dependencies",
"vscode:prepublish": "pnpm lint && pnpm build && pnpm bundle",
"vscode:package": "vsce package --no-dependencies",
"build": "pnpm langium:generate && tsc --noEmit",
"bundle": "node build/bundle.js",
"ts:watch": "tsc --watch --noEmit",
"tsc-alias:watch": "tsc-alias --watch",
"lint": "eslint src --ext ts",
"langium:generate": "langium generate",
"langium:watch": "langium generate --watch",
"watch": "concurrently --kill-others \"npm:langium:watch\" \"npm:ts:watch\" \"npm:tsc-alias:watch\"",
"test": "jest",
"prepublishOnly": "pnpm build"
"prepublishOnly": "pnpm build && pnpm bundle"
},
"dependencies": {
"@zenstackhq/internal": "workspace:*",
Expand All @@ -73,6 +93,7 @@
"vscode-jsonrpc": "^8.0.2",
"vscode-languageclient": "^8.0.2",
"vscode-languageserver": "^8.0.2",
"vscode-languageserver-textdocument": "^1.0.7",
"vscode-uri": "^3.0.6"
},
"devDependencies": {
Expand All @@ -86,6 +107,7 @@
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"concurrently": "^7.4.0",
"esbuild": "^0.15.12",
"eslint": "^7.32.0",
"jest": "^29.2.1",
"langium-cli": "^0.5.0",
Expand All @@ -94,6 +116,7 @@
"ts-node": "^10.9.1",
"tsc-alias": "^1.7.0",
"tsconfig-paths-jest": "^0.0.1",
"typescript": "^4.8.4"
"typescript": "^4.8.4",
"vsce": "^2.13.0"
}
}
8 changes: 7 additions & 1 deletion packages/schema/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import fs from 'fs';
import { AstNode, LangiumDocument, LangiumServices } from 'langium';
import { URI } from 'vscode-uri';
import { STD_LIB_MODULE_NAME } from '@lang/constants';

export async function extractDocument(
fileName: string,
Expand All @@ -23,8 +24,13 @@ export async function extractDocument(

const stdLib =
services.shared.workspace.LangiumDocuments.getOrCreateDocument(
URI.file(path.join(__dirname, '../language-server/stdlib.zmodel'))
URI.file(
path.resolve(
path.join(__dirname, '../res', STD_LIB_MODULE_NAME)
)
)
);

const document =
services.shared.workspace.LangiumDocuments.getOrCreateDocument(
URI.file(path.resolve(fileName))
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function deactivate(): Thenable<void> | undefined {

function startLanguageClient(context: vscode.ExtensionContext): LanguageClient {
const serverModule = context.asAbsolutePath(
path.join('out', 'language-server', 'main')
path.join('bundle', 'language-server', 'main')
);
// The debug options for the server
// --inspect=6009: runs the server in Node's Inspector mode so VS Code can attach to the server for debugging.
Expand Down
2 changes: 2 additions & 0 deletions packages/schema/src/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class ZenStackGenerator {
// generate package.json
const packageJson = require(path.join(
__dirname,
'../res',
'package.template.json'
));
fs.writeFileSync(
Expand All @@ -61,6 +62,7 @@ export class ZenStackGenerator {
// compile ts sources
const tsConfig = require(path.join(
__dirname,
'../res',
'tsconfig.template.json'
));
fs.writeFileSync(
Expand Down
2 changes: 2 additions & 0 deletions packages/schema/src/language-server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export const SCALAR_TYPES = [
'Bytes',
'DateTime',
];

export const STD_LIB_MODULE_NAME = 'stdlib.zmodel';
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {
CstNode,
DefinitionProvider,
findDeclarationNodeAtOffset,
getDocument,
GoToLink,
LangiumDocument,
LangiumServices,
MaybePromise,
NameProvider,
} from 'langium';
import { GrammarConfig } from 'langium/lib/grammar/grammar-config';
import { References } from 'langium/lib/references/references';
import { DefinitionParams, LocationLink } from 'vscode-languageserver';

export default class ZModelDefinitionProvider implements DefinitionProvider {
protected readonly nameProvider: NameProvider;
protected readonly references: References;
protected readonly grammarConfig: GrammarConfig;

constructor(services: LangiumServices) {
this.nameProvider = services.references.NameProvider;
this.references = services.references.References;
this.grammarConfig = services.parser.GrammarConfig;
}

getDefinition(
document: LangiumDocument,
params: DefinitionParams
): MaybePromise<LocationLink[] | undefined> {
const rootNode = document.parseResult.value;
if (rootNode.$cstNode) {
const cst = rootNode.$cstNode;
const sourceCstNode = findDeclarationNodeAtOffset(
cst,
document.textDocument.offsetAt(params.position),
this.grammarConfig.nameRegexp
);
if (sourceCstNode) {
return this.collectLocationLinks(sourceCstNode, params);
}
}
return undefined;
}

protected collectLocationLinks(
sourceCstNode: CstNode,
_params: DefinitionParams
): MaybePromise<LocationLink[] | undefined> {
const goToLink = this.findLink(sourceCstNode);
if (goToLink) {
if (
!goToLink.targetDocument.textDocument.uri.endsWith(
'stdlib.zmodel'
)
) {
return [
LocationLink.create(
goToLink.targetDocument.textDocument.uri,
(goToLink.target.element.$cstNode ?? goToLink.target)
.range,
goToLink.target.range,
goToLink.source.range
),
];
}
}
return undefined;
}

protected findLink(source: CstNode): GoToLink | undefined {
const target = this.references.findDeclarationNode(source);
if (target?.element) {
const targetDocument = getDocument(target.element);
if (target && targetDocument) {
return { source, target, targetDocument };
}
}
return undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default class DataModelValidator implements AstValidator<DataModel> {
) {
const decl = attr.decl.ref;
if (!decl) {
throw new Error(`Reference unresolved: ${attr.decl.$refText}`);
return;
}

const filledParams = new Set<AttributeParam>();
Expand Down Expand Up @@ -245,9 +245,9 @@ export default class DataModelValidator implements AstValidator<DataModel> {
'error',
`Fields ${oppositeFields
.map((f) => '"' + f.name + '"')
.join(', ')} on model ${
.join(', ')} on model "${
oppositeModel.name
} refer to the same relation to model "${
}" refer to the same relation to model "${
field.$container.name
}"`,
{ node: f }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { STD_LIB_MODULE_NAME } from '@lang/constants';
import { isDataSource, Model } from '@lang/generated/ast';
import { AstValidator } from '@lang/types';
import { ValidationAcceptor } from 'langium';
Expand All @@ -6,7 +7,10 @@ import { validateDuplicatedDeclarations } from './utils';
export default class SchemaValidator implements AstValidator<Model> {
validate(model: Model, accept: ValidationAcceptor): void {
validateDuplicatedDeclarations(model.declarations, accept);
this.validateDataSources(model, accept);

if (!model.$document?.uri.path.endsWith(STD_LIB_MODULE_NAME)) {
this.validateDataSources(model, accept);
}
}

private validateDataSources(model: Model, accept: ValidationAcceptor) {
Expand Down
8 changes: 6 additions & 2 deletions packages/schema/src/language-server/validator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ export function typeAssignable(
case 'Any':
return true;
case 'Float':
return sourceType === 'Int' || sourceType === 'Float';
return (
sourceType === 'Any' ||
sourceType === 'Int' ||
sourceType === 'Float'
);
default:
return sourceType === destType;
return sourceType === 'Any' || sourceType === destType;
}
}

Expand Down
Loading