Skip to content

Commit

Permalink
Добавил watch за схемой данных
Browse files Browse the repository at this point in the history
Fix #5
  • Loading branch information
tihonove committed Nov 6, 2018
1 parent b6f8160 commit d0cf4f6
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 21 deletions.
2 changes: 1 addition & 1 deletion client/src/SugarLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function startSugarLanguageServer(context: ExtensionContext): LanguageCli
const clientOptions: LanguageClientOptions = {
documentSelector: [{ scheme: "file", language: "sugar-xml" }, { scheme: "file", language: "xml" }],
synchronize: {
fileEvents: workspace.createFileSystemWatcher("**/.clientrc"),
fileEvents: workspace.createFileSystemWatcher("**/*.rng.xml"),
},
};

Expand Down
24 changes: 17 additions & 7 deletions server/src/LanguageServer/SugarDocumentIntellisenseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface IDocumentOffsetPositionResolver {

export class SugarDocumentIntellisenseService {
private readonly logger: ILogger;
private readonly parsedSchemaDocuments: DataSchemaElementNode;
private parsedSchemaDocument: DataSchemaElementNode;
private readonly validator: SugarValidator;
private readonly documentUri: string;
private readonly schemaFileUri: string;
Expand All @@ -82,15 +82,25 @@ export class SugarDocumentIntellisenseService {
this.logger = logger;
this.offsetPositionResolver = offsetPositionResolver;
this.schemaFileUri = UriUtils.fileNameToUri(this.findSchemaFile(this.documentUri));
this.parsedSchemaDocuments = this.loadDataSchema();
this.suggester = new CompletionSuggester([], allElements, this.parsedSchemaDocuments);
this.parsedSchemaDocument = this.loadDataSchema();
this.suggester = new CompletionSuggester([], allElements, this.parsedSchemaDocument);
this.sugarElements = allElements;
this.validator = createDefaultValidator(this.parsedSchemaDocuments);
this.validator = createDefaultValidator(this.parsedSchemaDocument);
this.builder = new OffsetToNodeMapBuilder();
this.typeInfoExtractor = new TypeInfoExtractor();
this.helpUrlBuilder = new HelpUrlBuilder(sugarElementsGroups);
}

public updateSchema(): void {
this.parsedSchemaDocument = this.loadDataSchema();
this.validator.updateDataSchema(this.parsedSchemaDocument);
this.suggester.updateDataSchema(this.parsedSchemaDocument);
}

public isLinkedWithSchemaFile(uri: string): boolean {
return this.schemaFileUri === uri;
}

public getCodeLenses(): undefined | CodeLens[] {
if (this.userDefinedTypeUsagesInfo != undefined) {
return this.userDefinedTypeUsagesInfo.map<CodeLens>(x => ({
Expand Down Expand Up @@ -432,7 +442,7 @@ export class SugarDocumentIntellisenseService {

if (suggestionItem.type === SuggestionItemType.DataElement) {
const dataSchemaNode = DataSchemaUtils.findElementByPath(
this.parsedSchemaDocuments,
this.parsedSchemaDocument,
suggestionItem.fullPath
);
if (dataSchemaNode == undefined) {
Expand All @@ -451,7 +461,7 @@ export class SugarDocumentIntellisenseService {

if (suggestionItem.type === SuggestionItemType.DataAttribute) {
const dataSchemaAttribute = DataSchemaUtils.findAttributeByPath(
this.parsedSchemaDocuments,
this.parsedSchemaDocument,
suggestionItem.fullPath
);
if (dataSchemaAttribute == undefined) {
Expand Down Expand Up @@ -535,7 +545,7 @@ export class SugarDocumentIntellisenseService {
}

private getDataElementOrAttributeByPath(path: string[]): undefined | DataSchemaNode {
return DataSchemaUtils.findSchemaNodeByPath(this.parsedSchemaDocuments, path);
return DataSchemaUtils.findSchemaNodeByPath(this.parsedSchemaDocument, path);
}

private resolveContextAsOffset(offset: number): undefined | CodeContext {
Expand Down
21 changes: 21 additions & 0 deletions server/src/LanguageServer/SugarLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
CompletionItem,
Connection,
createConnection,
DidChangeConfigurationNotification,
DidChangeWatchedFilesParams,
InitializeParams,
InitializeResult,
Location,
Expand Down Expand Up @@ -75,8 +77,27 @@ export class SugarLanguageServer {
public listen(): void {
this.documents.listen(this.connection);
this.connection.listen();
this.connection.client.register(DidChangeConfigurationNotification.type, undefined);
this.connection.onDidChangeWatchedFiles(this.handleChangeWatchedFiles);
}

private readonly handleChangeWatchedFiles = (params: DidChangeWatchedFilesParams): void => {
for (const documentUri of Object.keys(this.documentServices)) {
const documentService = this.documentServices[documentUri];
if (documentService != undefined) {
for (const uri of params.changes.map(x => x.uri)) {
if (documentService.isLinkedWithSchemaFile(uri)) {
documentService.updateSchema();
const textDocument = this.documents.get(documentUri);
if (textDocument != undefined) {
documentService.validateTextDocument(textDocument.getText());
}
}
}
}
}
};

private readonly handleFindAllReferences = (params: ReferenceParams): undefined | Location[] => {
const documentService = this.documentServices[params.textDocument.uri];
if (documentService != undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface Suggestions {
export class CompletionSuggester {
private readonly sugarElementInfos: SugarElementInfo[];
private sugarTypes: UserDefinedSugarTypeInfo[];
private readonly dataSchemaRoot: DataSchemaElementNode;
private dataSchemaRoot: DataSchemaElementNode;
private readonly contextResolver: CodeContextByNodeResolver;

public constructor(
Expand All @@ -39,6 +39,10 @@ export class CompletionSuggester {
this.contextResolver = new CodeContextByNodeResolver(this.sugarElementInfos);
}

public updateDataSchema(dataSchema: DataSchemaElementNode): void {
this.dataSchemaRoot = dataSchema;
}

public updateUserDefinedSugarType(sugarTypes: UserDefinedSugarTypeInfo[]): void {
this.sugarTypes = sugarTypes;
}
Expand Down
1 change: 1 addition & 0 deletions server/src/Utils/PegJSUtils/SubscriptionsTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type RuleHandler<TResult> = (result: TResult, location: CodePosition, act

export class SubscriptionsTracer implements IPegJSTracer {
private handlers: {
// tslint:disable-next-line:no-any
[ruleName: string]: undefined | { [value in RuleAction]?: RuleHandler<any> };
} = {};

Expand Down
7 changes: 5 additions & 2 deletions server/src/Validator/Rules/ValidPathRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { ValidationItem } from "./Bases/ValidationItem";
export class ValidPathRule extends SugarValidatorRuleBase {
private readonly elementInfos: SugarElementInfo[];
private readonly validations: ValidationItem[] = [];
private readonly dataSchema: DataSchemaElementNode;
private readonly dataSchema: undefined | DataSchemaElementNode;
public pathScopeStack: string[][] = [];

public constructor(dataSchema: DataSchemaElementNode, elementInfos: SugarElementInfo[]) {
public constructor(dataSchema: undefined | DataSchemaElementNode, elementInfos: SugarElementInfo[]) {
super("valid-path");
this.dataSchema = dataSchema;
this.elementInfos = elementInfos;
Expand All @@ -37,6 +37,9 @@ export class ValidPathRule extends SugarValidatorRuleBase {
const attributeName = attribute.name;
const element = attribute.parent;
const elementInfo = this.elementInfos.find(x => x.name === element.name.value);
if (this.dataSchema == undefined) {
return;
}
if (attribute.value == undefined) {
return;
}
Expand Down
16 changes: 13 additions & 3 deletions server/src/Validator/Validator/SugarValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NullTracer } from "../../Utils/PegJSUtils/NullTracer";
import { ISugarValidatorRule } from "../Rules/Bases/ISugarValidatorRule";
import { ValidationItem } from "../Rules/Bases/ValidationItem";

import { DataSchemaElementNode } from "../../DataSchema/DataSchemaNode";
import { traverseSugar } from "../../SugarAnalyzing/Traversing/TraverseSugar";
import { TypeInfoExtractor } from "../../SugarAnalyzing/TypeInfoExtraction/TypeInfoExtractor";
import { UserDefinedSugarTypeInfo } from "../../SugarElements/UserDefinedSugarTypeInfo";
Expand All @@ -19,18 +20,24 @@ export interface ValidationReportItem extends ValidationItem {
export class SugarValidator {
private readonly rules: SugarValidatorRuleFactory[] = [];
private readonly typeInfoExtractor: TypeInfoExtractor;
private dataSchema: undefined | DataSchemaElementNode;

public constructor() {
public constructor(dataSchema: undefined | DataSchemaElementNode) {
this.dataSchema = dataSchema;
this.typeInfoExtractor = new TypeInfoExtractor();
}

public updateDataSchema(dataSchema: undefined | DataSchemaElementNode): void {
this.dataSchema = dataSchema;
}

public validate(input: string): ValidationReportItem[] {
try {
const parseResult = parseSugar(input, { tracer: new NullTracer() });
const userDefinedTypeInfos = this.typeInfoExtractor.extractTypeInfos(parseResult);
const validations: ValidationReportItem[] = [];
for (const ruleFactory of this.rules) {
const rule = ruleFactory(userDefinedTypeInfos);
const rule = ruleFactory(userDefinedTypeInfos, this.dataSchema);
rule.beforeProcess(parseResult);
validations.push(...this.processRule(rule, parseResult));
}
Expand Down Expand Up @@ -63,4 +70,7 @@ export class SugarValidator {
}
}

export type SugarValidatorRuleFactory = (userDefinedTypeInfos: UserDefinedSugarTypeInfo[]) => ISugarValidatorRule;
export type SugarValidatorRuleFactory = (
userDefinedTypeInfos: UserDefinedSugarTypeInfo[],
dataSchema: undefined | DataSchemaElementNode
) => ISugarValidatorRule;
6 changes: 2 additions & 4 deletions server/src/Validator/ValidatorFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import { ValidTypeRule } from "./Rules/ValidTypeRule";
import { SugarValidator } from "./Validator/SugarValidator";

export function createDefaultValidator(dataSchema: undefined | DataSchemaElementNode): SugarValidator {
const sugarValidator = new SugarValidator();
const sugarValidator = new SugarValidator(dataSchema);
sugarValidator.addRule(() => new ValidElementRule(allElements));
sugarValidator.addRule(() => new ValidAttributeRule(allElements));
sugarValidator.addRule(() => new RequiredAttributesRule(allElements));
sugarValidator.addRule(() => new ValidAttributeValueType(allElements));
sugarValidator.addRule(userDefinedTypes => new ValidTypeRule(userDefinedTypes, allElements));
sugarValidator.addRule(userDefinedTypes => new NoUnusedTypesRule(userDefinedTypes, allElements));
if (dataSchema != undefined) {
sugarValidator.addRule(() => new ValidPathRule(dataSchema, allElements));
}
sugarValidator.addRule((_x, dataSchema) => new ValidPathRule(dataSchema, allElements));
return sugarValidator;
}
2 changes: 1 addition & 1 deletion test/src/Validator/SugarValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { testSugarElementInfos } from "../Utils/TestInfos";
export class SugarValidatorTest {
@test
public testSyntaxError(): void {
const validator = new SugarValidator();
const validator = new SugarValidator(undefined);
validator.addRule(() => new ValidElementRule(testSugarElementInfos));

expect(validator.validate("<not ")).to.shallowDeepEqual([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { SugarValidator } from "../../../../../server/src/Validator/Validator/Su

export class SugarValidationRuleTestBase {
protected assertValidCode(input: string): void {
const validator = new SugarValidator();
const validator = new SugarValidator(undefined);
validator.addRule(userDefinedTypes => this.createRule(userDefinedTypes));

expect(validator.validate(input)).to.eql([]);
}

protected assertInvalidCode(input: string, ...errors: ValidationReportItemAssert[]): void {
const validator = new SugarValidator();
const validator = new SugarValidator(undefined);
validator.addRule(userDefinedTypes => this.createRule(userDefinedTypes));

if (errors.length === 0) {
Expand Down

0 comments on commit d0cf4f6

Please sign in to comment.