UNPKG

@eclipse-glsp/client

Version:

A sprotty-based client for GLSP

217 lines 10.4 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeleteMarkersCommand = exports.ApplyMarkersCommand = exports.ApplyMarkersAction = exports.SetMarkersActionHandler = exports.ExternalMarkerManager = exports.ValidationFeedbackEmitter = void 0; /******************************************************************************** * Copyright (c) 2019-2024 EclipseSource and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * This Source Code may also be made available under the following Secondary * Licenses when the conditions for such availability set forth in the Eclipse * Public License v. 2.0 are satisfied: GNU General Public License, version 2 * with the GNU Classpath Exception which is available at * https://www.gnu.org/software/classpath/license.html. * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ const sprotty_1 = require("@eclipse-glsp/sprotty"); const inversify_1 = require("inversify"); const editor_context_service_1 = require("../../base/editor-context-service"); const feedback_command_1 = require("../../base/feedback/feedback-command"); const gmodel_util_1 = require("../../utils/gmodel-util"); const issue_marker_1 = require("./issue-marker"); /** * Feedback emitter sending actions for visualizing model validation feedback and * re-establishing this feedback visualization after the model has been updated. */ let ValidationFeedbackEmitter = class ValidationFeedbackEmitter { constructor() { this.registeredFeedbackByReason = new Map(); } /** * Register the action that should be emitted for visualizing validation feedback. * @param action the action that should be emitted when the model is updated and that will visualize the model validation feedback. * @param reason the reason for this validation feedback. */ registerValidationFeedbackAction(action, reason = '') { var _a, _b; // De-register feedback and clear existing markers with the same reason const emitter = (_b = (_a = this.registeredFeedbackByReason.get(reason)) === null || _a === void 0 ? void 0 : _a.dispose()) !== null && _b !== void 0 ? _b : this.feedbackActionDispatcher.createEmitter(); emitter.add(action, () => sprotty_1.DeleteMarkersAction.create(action.markers)).submit(); this.registeredFeedbackByReason.set(reason, emitter); } }; exports.ValidationFeedbackEmitter = ValidationFeedbackEmitter; __decorate([ (0, inversify_1.inject)(sprotty_1.TYPES.IFeedbackActionDispatcher), __metadata("design:type", Object) ], ValidationFeedbackEmitter.prototype, "feedbackActionDispatcher", void 0); exports.ValidationFeedbackEmitter = ValidationFeedbackEmitter = __decorate([ (0, inversify_1.injectable)() ], ValidationFeedbackEmitter); /** * Manages current markers for the outside of the GLSP. * * Typically this is rebound by the surrounding tool, e.g. Theia, to be aware of * and propagate current markers. */ let ExternalMarkerManager = class ExternalMarkerManager { connect(actionDispatcher) { this.actionDispatcher = actionDispatcher; } removeMarkers(markers) { if (this.actionDispatcher) { this.actionDispatcher.dispatch(sprotty_1.DeleteMarkersAction.create(markers)); } } }; exports.ExternalMarkerManager = ExternalMarkerManager; exports.ExternalMarkerManager = ExternalMarkerManager = __decorate([ (0, inversify_1.injectable)() ], ExternalMarkerManager); let SetMarkersActionHandler = class SetMarkersActionHandler { handle(action) { const markers = action.markers; this.setMarkers(markers, action.reason); } async setMarkers(markers, reason) { var _a; const uri = this.editorContextService.sourceUri; (_a = this.externalMarkerManager) === null || _a === void 0 ? void 0 : _a.setMarkers(markers, reason, uri); const applyMarkersAction = ApplyMarkersAction.create(markers); this.validationFeedbackEmitter.registerValidationFeedbackAction(applyMarkersAction, reason); } }; exports.SetMarkersActionHandler = SetMarkersActionHandler; __decorate([ (0, inversify_1.inject)(ValidationFeedbackEmitter), __metadata("design:type", ValidationFeedbackEmitter) ], SetMarkersActionHandler.prototype, "validationFeedbackEmitter", void 0); __decorate([ (0, inversify_1.inject)(ExternalMarkerManager), (0, inversify_1.optional)(), __metadata("design:type", ExternalMarkerManager) ], SetMarkersActionHandler.prototype, "externalMarkerManager", void 0); __decorate([ (0, inversify_1.inject)(editor_context_service_1.EditorContextService), __metadata("design:type", editor_context_service_1.EditorContextService) ], SetMarkersActionHandler.prototype, "editorContextService", void 0); exports.SetMarkersActionHandler = SetMarkersActionHandler = __decorate([ (0, inversify_1.injectable)() ], SetMarkersActionHandler); var ApplyMarkersAction; (function (ApplyMarkersAction) { ApplyMarkersAction.KIND = 'applyMarkers'; function is(object) { return sprotty_1.Action.hasKind(object, ApplyMarkersAction.KIND) && (0, sprotty_1.hasArrayProp)(object, 'markers'); } ApplyMarkersAction.is = is; function create(markers) { return { kind: ApplyMarkersAction.KIND, markers }; } ApplyMarkersAction.create = create; })(ApplyMarkersAction || (exports.ApplyMarkersAction = ApplyMarkersAction = {})); /** * Handles {@link ApplyMarkersAction}s by creating the corresponding {@link GIssueMarker}s and * adding them to the graphical model. */ let ApplyMarkersCommand = class ApplyMarkersCommand extends feedback_command_1.FeedbackCommand { constructor(action) { super(); this.action = action; } execute(context) { this.action.markers.forEach(marker => { const modelElement = context.root.index.getById(marker.elementId); if (modelElement instanceof sprotty_1.GParentElement) { const issueMarker = (0, issue_marker_1.getOrCreateGIssueMarker)(modelElement); const issue = (0, issue_marker_1.createGIssue)(marker); issueMarker.issues.push(issue); if (issueMarker instanceof issue_marker_1.GIssueMarker) { issueMarker.computeProjectionCssClasses(); } addMaxSeverityCSSClassToIssueParent(modelElement, issueMarker); } }); return context.root; } }; exports.ApplyMarkersCommand = ApplyMarkersCommand; ApplyMarkersCommand.KIND = ApplyMarkersAction.KIND; exports.ApplyMarkersCommand = ApplyMarkersCommand = __decorate([ (0, inversify_1.injectable)(), __param(0, (0, inversify_1.inject)(sprotty_1.TYPES.Action)), __metadata("design:paramtypes", [Object]) ], ApplyMarkersCommand); function addMaxSeverityCSSClassToIssueParent(modelElement, issueMarker) { const maxSeverityCSSClass = (0, issue_marker_1.getSeverity)(issueMarker); if (!modelElement.cssClasses) { modelElement.cssClasses = [maxSeverityCSSClass]; } else { modelElement.cssClasses = modelElement.cssClasses.filter((value) => !value.match('info|warning|error')); modelElement.cssClasses.push(maxSeverityCSSClass); } } function removeCSSClassFromIssueParent(modelElement, issueMarker) { const severity = (0, issue_marker_1.getSeverity)(issueMarker); (0, gmodel_util_1.removeCssClasses)(modelElement, [severity]); } /** * Command for handling `DeleteMarkersAction` */ let DeleteMarkersCommand = class DeleteMarkersCommand extends feedback_command_1.FeedbackCommand { constructor(action) { super(); this.action = action; } execute(context) { this.action.markers.forEach(marker => { const modelElement = context.root.index.getById(marker.elementId); if (modelElement instanceof sprotty_1.GParentElement) { const issueMarker = (0, issue_marker_1.getGIssueMarker)(modelElement); if (issueMarker) { removeCSSClassFromIssueParent(modelElement, issueMarker); for (let index = 0; index < issueMarker.issues.length; ++index) { const issue = issueMarker.issues[index]; if (issue.message === marker.description) { issueMarker.issues.splice(index--, 1); } } if (issueMarker.issues.length === 0) { modelElement.remove(issueMarker); } else { addMaxSeverityCSSClassToIssueParent(modelElement, issueMarker); } } } }); return context.root; } }; exports.DeleteMarkersCommand = DeleteMarkersCommand; DeleteMarkersCommand.KIND = sprotty_1.DeleteMarkersAction.KIND; exports.DeleteMarkersCommand = DeleteMarkersCommand = __decorate([ (0, inversify_1.injectable)(), __param(0, (0, inversify_1.inject)(sprotty_1.TYPES.Action)), __metadata("design:paramtypes", [Object]) ], DeleteMarkersCommand); //# sourceMappingURL=validate.js.map