@eclipse-glsp/server 1.1.0-next.3d7585d.42 → 1.1.0-next.44bd1e0.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/lib/common/command/command-stack.d.ts +38 -8
  2. package/lib/common/command/command-stack.d.ts.map +1 -1
  3. package/lib/common/command/command-stack.js +75 -6
  4. package/lib/common/command/command-stack.js.map +1 -1
  5. package/lib/common/command/command-stack.spec.d.ts +2 -0
  6. package/lib/common/command/command-stack.spec.d.ts.map +1 -0
  7. package/lib/common/command/command-stack.spec.js +207 -0
  8. package/lib/common/command/command-stack.spec.js.map +1 -0
  9. package/lib/common/command/command.d.ts +30 -4
  10. package/lib/common/command/command.d.ts.map +1 -1
  11. package/lib/common/command/command.js +69 -0
  12. package/lib/common/command/command.js.map +1 -1
  13. package/lib/common/command/command.spec.d.ts +2 -0
  14. package/lib/common/command/command.spec.d.ts.map +1 -0
  15. package/lib/common/command/command.spec.js +86 -0
  16. package/lib/common/command/command.spec.js.map +1 -0
  17. package/lib/common/command/recording-command.d.ts +75 -0
  18. package/lib/common/command/recording-command.d.ts.map +1 -0
  19. package/lib/common/command/recording-command.js +99 -0
  20. package/lib/common/command/recording-command.js.map +1 -0
  21. package/lib/common/command/recording-command.spec.d.ts +17 -0
  22. package/lib/common/command/recording-command.spec.d.ts.map +1 -0
  23. package/lib/common/command/recording-command.spec.js +62 -0
  24. package/lib/common/command/recording-command.spec.js.map +1 -0
  25. package/lib/common/command/undo-redo-action-handler.d.ts +28 -0
  26. package/lib/common/command/undo-redo-action-handler.d.ts.map +1 -0
  27. package/lib/common/command/undo-redo-action-handler.js +72 -0
  28. package/lib/common/command/undo-redo-action-handler.js.map +1 -0
  29. package/lib/common/di/diagram-module.d.ts +3 -0
  30. package/lib/common/di/diagram-module.d.ts.map +1 -1
  31. package/lib/common/di/diagram-module.js +12 -0
  32. package/lib/common/di/diagram-module.js.map +1 -1
  33. package/lib/common/features/contextactions/tool-palette-item-provider.d.ts +1 -1
  34. package/lib/common/features/contextactions/tool-palette-item-provider.d.ts.map +1 -1
  35. package/lib/common/features/contextactions/tool-palette-item-provider.js +2 -4
  36. package/lib/common/features/contextactions/tool-palette-item-provider.js.map +1 -1
  37. package/lib/common/{gmodel → features/layout}/computed-bounds-action-handler.d.ts +8 -3
  38. package/lib/common/features/layout/computed-bounds-action-handler.d.ts.map +1 -0
  39. package/lib/common/{gmodel → features/layout}/computed-bounds-action-handler.js +8 -3
  40. package/lib/common/features/layout/computed-bounds-action-handler.js.map +1 -0
  41. package/lib/common/features/layout/layout-operation-handler.d.ts +12 -3
  42. package/lib/common/features/layout/layout-operation-handler.d.ts.map +1 -1
  43. package/lib/common/features/layout/layout-operation-handler.js +26 -10
  44. package/lib/common/features/layout/layout-operation-handler.js.map +1 -1
  45. package/lib/common/features/model/model-state.d.ts +0 -2
  46. package/lib/common/features/model/model-state.d.ts.map +1 -1
  47. package/lib/common/features/model/model-state.js +0 -1
  48. package/lib/common/features/model/model-state.js.map +1 -1
  49. package/lib/common/features/model/model-submission-handler.d.ts +2 -0
  50. package/lib/common/features/model/model-submission-handler.d.ts.map +1 -1
  51. package/lib/common/features/model/model-submission-handler.js +7 -2
  52. package/lib/common/features/model/model-submission-handler.js.map +1 -1
  53. package/lib/common/features/model/save-model-action-handler.d.ts +2 -2
  54. package/lib/common/features/model/save-model-action-handler.d.ts.map +1 -1
  55. package/lib/common/features/model/save-model-action-handler.js +5 -5
  56. package/lib/common/features/model/save-model-action-handler.js.map +1 -1
  57. package/lib/common/gmodel/apply-label-edit-operation-handler.d.ts +4 -5
  58. package/lib/common/gmodel/apply-label-edit-operation-handler.d.ts.map +1 -1
  59. package/lib/common/gmodel/apply-label-edit-operation-handler.js +12 -21
  60. package/lib/common/gmodel/apply-label-edit-operation-handler.js.map +1 -1
  61. package/lib/common/gmodel/change-bounds-operation-handler.d.ts +9 -6
  62. package/lib/common/gmodel/change-bounds-operation-handler.d.ts.map +1 -1
  63. package/lib/common/gmodel/change-bounds-operation-handler.js +14 -14
  64. package/lib/common/gmodel/change-bounds-operation-handler.js.map +1 -1
  65. package/lib/common/gmodel/change-routing-points-operation-handler.d.ts +5 -5
  66. package/lib/common/gmodel/change-routing-points-operation-handler.d.ts.map +1 -1
  67. package/lib/common/gmodel/change-routing-points-operation-handler.js +11 -14
  68. package/lib/common/gmodel/change-routing-points-operation-handler.js.map +1 -1
  69. package/lib/common/{operations → gmodel}/cut-operation-handler.d.ts +5 -4
  70. package/lib/common/gmodel/cut-operation-handler.d.ts.map +1 -0
  71. package/lib/common/{operations → gmodel}/cut-operation-handler.js +13 -11
  72. package/lib/common/gmodel/cut-operation-handler.js.map +1 -0
  73. package/lib/common/gmodel/delete-operation-handler.d.ts +6 -6
  74. package/lib/common/gmodel/delete-operation-handler.d.ts.map +1 -1
  75. package/lib/common/gmodel/delete-operation-handler.js +13 -10
  76. package/lib/common/gmodel/delete-operation-handler.js.map +1 -1
  77. package/lib/common/gmodel/gmodel-create-edge-operation-handler.d.ts +11 -7
  78. package/lib/common/gmodel/gmodel-create-edge-operation-handler.d.ts.map +1 -1
  79. package/lib/common/gmodel/gmodel-create-edge-operation-handler.js +12 -13
  80. package/lib/common/gmodel/gmodel-create-edge-operation-handler.js.map +1 -1
  81. package/lib/common/gmodel/gmodel-create-node-operation-handler.d.ts +32 -5
  82. package/lib/common/gmodel/gmodel-create-node-operation-handler.d.ts.map +1 -1
  83. package/lib/common/gmodel/gmodel-create-node-operation-handler.js +45 -3
  84. package/lib/common/gmodel/gmodel-create-node-operation-handler.js.map +1 -1
  85. package/lib/common/gmodel/gmodel-diagram-module.d.ts +8 -8
  86. package/lib/common/gmodel/gmodel-diagram-module.d.ts.map +1 -1
  87. package/lib/common/gmodel/gmodel-diagram-module.js +15 -21
  88. package/lib/common/gmodel/gmodel-diagram-module.js.map +1 -1
  89. package/lib/common/gmodel/gmodel-operation-handler.d.ts +23 -0
  90. package/lib/common/gmodel/gmodel-operation-handler.d.ts.map +1 -0
  91. package/lib/common/gmodel/gmodel-operation-handler.js +43 -0
  92. package/lib/common/gmodel/gmodel-operation-handler.js.map +1 -0
  93. package/lib/common/gmodel/index.d.ts +2 -1
  94. package/lib/common/gmodel/index.d.ts.map +1 -1
  95. package/lib/common/gmodel/index.js +2 -1
  96. package/lib/common/gmodel/index.js.map +1 -1
  97. package/lib/common/gmodel/paste-operation-handler.d.ts +7 -6
  98. package/lib/common/gmodel/paste-operation-handler.d.ts.map +1 -1
  99. package/lib/common/gmodel/paste-operation-handler.js +20 -15
  100. package/lib/common/gmodel/paste-operation-handler.js.map +1 -1
  101. package/lib/common/gmodel/reconnect-edge-operation-handler.d.ts +5 -5
  102. package/lib/common/gmodel/reconnect-edge-operation-handler.d.ts.map +1 -1
  103. package/lib/common/gmodel/reconnect-edge-operation-handler.js +11 -14
  104. package/lib/common/gmodel/reconnect-edge-operation-handler.js.map +1 -1
  105. package/lib/common/index.d.ts +2 -1
  106. package/lib/common/index.d.ts.map +1 -1
  107. package/lib/common/index.js +2 -1
  108. package/lib/common/index.js.map +1 -1
  109. package/lib/common/operations/compound-operation-handler.d.ts +5 -5
  110. package/lib/common/operations/compound-operation-handler.d.ts.map +1 -1
  111. package/lib/common/operations/compound-operation-handler.js +37 -9
  112. package/lib/common/operations/compound-operation-handler.js.map +1 -1
  113. package/lib/common/operations/create-operation-handler.d.ts +8 -40
  114. package/lib/common/operations/create-operation-handler.d.ts.map +1 -1
  115. package/lib/common/operations/create-operation-handler.js +26 -85
  116. package/lib/common/operations/create-operation-handler.js.map +1 -1
  117. package/lib/common/operations/operation-action-handler.d.ts +10 -1
  118. package/lib/common/operations/operation-action-handler.d.ts.map +1 -1
  119. package/lib/common/operations/operation-action-handler.js +37 -22
  120. package/lib/common/operations/operation-action-handler.js.map +1 -1
  121. package/lib/common/operations/operation-handler-registry.d.ts +11 -2
  122. package/lib/common/operations/operation-handler-registry.d.ts.map +1 -1
  123. package/lib/common/operations/operation-handler-registry.js +31 -2
  124. package/lib/common/operations/operation-handler-registry.js.map +1 -1
  125. package/lib/common/operations/operation-handler.d.ts +28 -11
  126. package/lib/common/operations/operation-handler.d.ts.map +1 -1
  127. package/lib/common/operations/operation-handler.js +50 -1
  128. package/lib/common/operations/operation-handler.js.map +1 -1
  129. package/lib/common/test/mock-util.d.ts +7 -0
  130. package/lib/common/test/mock-util.d.ts.map +1 -1
  131. package/lib/common/test/mock-util.js +20 -37
  132. package/lib/common/test/mock-util.js.map +1 -1
  133. package/package.json +4 -3
  134. package/src/common/command/command-stack.spec.ts +239 -0
  135. package/src/common/command/command-stack.ts +99 -14
  136. package/src/common/command/command.spec.ts +93 -0
  137. package/src/common/command/command.ts +89 -4
  138. package/src/common/command/recording-command.spec.ts +74 -0
  139. package/src/common/command/recording-command.ts +119 -0
  140. package/src/common/command/undo-redo-action-handler.ts +57 -0
  141. package/src/common/di/diagram-module.ts +13 -0
  142. package/src/common/features/contextactions/tool-palette-item-provider.ts +2 -4
  143. package/src/common/{gmodel → features/layout}/computed-bounds-action-handler.ts +9 -4
  144. package/src/common/features/layout/layout-operation-handler.ts +26 -11
  145. package/src/common/features/model/model-state.ts +0 -3
  146. package/src/common/features/model/model-submission-handler.ts +6 -2
  147. package/src/common/features/model/save-model-action-handler.ts +5 -6
  148. package/src/common/gmodel/apply-label-edit-operation-handler.ts +13 -15
  149. package/src/common/gmodel/change-bounds-operation-handler.ts +12 -8
  150. package/src/common/gmodel/change-routing-points-operation-handler.ts +8 -7
  151. package/src/common/{operations → gmodel}/cut-operation-handler.ts +9 -8
  152. package/src/common/gmodel/delete-operation-handler.ts +16 -10
  153. package/src/common/gmodel/gmodel-create-edge-operation-handler.ts +16 -10
  154. package/src/common/gmodel/gmodel-create-node-operation-handler.ts +54 -6
  155. package/src/common/gmodel/gmodel-diagram-module.ts +22 -27
  156. package/src/common/gmodel/gmodel-operation-handler.ts +31 -0
  157. package/src/common/gmodel/index.ts +2 -1
  158. package/src/common/gmodel/paste-operation-handler.ts +19 -11
  159. package/src/common/gmodel/reconnect-edge-operation-handler.ts +8 -7
  160. package/src/common/index.ts +2 -1
  161. package/src/common/operations/compound-operation-handler.ts +14 -12
  162. package/src/common/operations/create-operation-handler.ts +17 -74
  163. package/src/common/operations/operation-action-handler.ts +51 -27
  164. package/src/common/operations/operation-handler-registry.ts +18 -3
  165. package/src/common/operations/operation-handler.ts +38 -13
  166. package/src/common/test/mock-util.ts +21 -36
  167. package/lib/common/gmodel/apply-label-edit-operation-handler.spec.d.ts +0 -2
  168. package/lib/common/gmodel/apply-label-edit-operation-handler.spec.d.ts.map +0 -1
  169. package/lib/common/gmodel/apply-label-edit-operation-handler.spec.js +0 -38
  170. package/lib/common/gmodel/apply-label-edit-operation-handler.spec.js.map +0 -1
  171. package/lib/common/gmodel/computed-bounds-action-handler.d.ts.map +0 -1
  172. package/lib/common/gmodel/computed-bounds-action-handler.js.map +0 -1
  173. package/lib/common/operations/cut-operation-handler.d.ts.map +0 -1
  174. package/lib/common/operations/cut-operation-handler.js.map +0 -1
  175. package/src/common/gmodel/apply-label-edit-operation-handler.spec.ts +0 -40
@@ -13,94 +13,37 @@
13
13
  *
14
14
  * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
15
  ********************************************************************************/
16
- import { GModelElement } from '@eclipse-glsp/graph';
17
16
  import {
18
17
  CreateEdgeOperation,
19
18
  CreateNodeOperation,
20
- CreateOperation,
21
- MaybePromise,
22
- Operation,
23
- Point,
19
+ hasArrayProp,
20
+ hasFunctionProp,
21
+ hasStringProp,
24
22
  TriggerEdgeCreationAction,
25
23
  TriggerNodeCreationAction
26
24
  } from '@eclipse-glsp/protocol';
27
- import { inject, injectable } from 'inversify';
28
- import { ModelState } from '../features/model/model-state';
29
- import { getRelativeLocation } from '../utils/layout-util';
30
25
  import { OperationHandler } from './operation-handler';
31
26
 
32
27
  /**
33
28
  * A special {@link OperationHandler} that is responsible for the handling of {@link CreateOperation}s. Depending on its
34
29
  * operation type the triggered actions are {@link TriggerNodeCreationAction} or {@link TriggerEdgeCreationAction}s.
35
30
  */
36
- @injectable()
37
- export abstract class CreateOperationHandler implements OperationHandler {
38
- abstract operationType: string;
39
- abstract readonly label: string;
40
- abstract elementTypeIds: string[];
41
-
42
- abstract execute(operation: Operation): MaybePromise<void>;
43
-
44
- /**
45
- * Returns a list of {@link TriggerElementCreationAction}s for registered element types.
46
- *
47
- * @returns A list of {@link TriggerElementCreationAction}s.
48
- */
49
- getTriggerActions(): (TriggerEdgeCreationAction | TriggerNodeCreationAction)[] {
50
- if (this.operationType === CreateNodeOperation.KIND) {
51
- return this.elementTypeIds.map(typeId => TriggerNodeCreationAction.create(typeId));
52
- } else if (this.operationType === CreateEdgeOperation.KIND) {
53
- return this.elementTypeIds.map(typeId => TriggerEdgeCreationAction.create(typeId));
54
- }
55
- return [];
56
- }
57
-
58
- handles(operation: CreateOperation): boolean {
59
- return this.elementTypeIds.includes(operation.elementTypeId);
60
- }
31
+ export interface CreateOperationHandler extends OperationHandler {
32
+ readonly label: string;
33
+ elementTypeIds: string[];
34
+ operationType: CreateOperationKind;
35
+ getTriggerActions(): (TriggerEdgeCreationAction | TriggerNodeCreationAction)[];
61
36
  }
62
37
 
63
- /**
64
- * Abstract base implementation for {@link CreateOperationHandler} that
65
- * create an element that is represented with a {@link GNode} in the graphical model.
66
- */
67
- @injectable()
68
- export abstract class CreateNodeOperationHandler extends CreateOperationHandler {
69
- @inject(ModelState)
70
- protected modelState: ModelState;
71
-
72
- get operationType(): string {
73
- return CreateNodeOperation.KIND;
74
- }
75
-
76
- /**
77
- * Retrieve the graphical model element that should contain the newly created {@link GNode}.
78
- * If `undefined` is returned the {@link GNode} should be added directly to the diagram root.
79
- * @param operation The currently handled operation.
80
- * @returns The container {@link GModeElement} or `undefined`.
81
- */
82
- getContainer(operation: CreateNodeOperation): GModelElement | undefined {
83
- const index = this.modelState.index;
84
- return operation.containerId ? index.get(operation.containerId) : undefined;
85
- }
86
-
87
- getLocation(operation: CreateNodeOperation): Point | undefined {
88
- return operation.location;
89
- }
38
+ export type CreateOperationKind = typeof CreateNodeOperation.KIND | typeof CreateEdgeOperation.KIND;
90
39
 
91
- /**
92
- * Retrieves the diagram absolute location and the target container from the given {@link CreateNodeOperation}
93
- * and converts the absolute location to coordinates relative to the given container.
94
- * Relative coordinates can only be retrieved if the given container element is part of
95
- * a hierarchy of {@link GBoundsAware} elements. This means each (recursive) parent element need to
96
- * implement {@link GBoundsAware}. If that is not the case this method returns `undefined`.
97
- * @param absoluteLocation The diagram absolute position.
98
- * @param container The container element.
99
- * @returns The relative position or `undefined`.
100
- */
101
- getRelativeLocation(operation: CreateNodeOperation): Point | undefined {
102
- const container = this.getContainer(operation) ?? this.modelState.root;
103
- const absoluteLocation = this.getLocation(operation) ?? Point.ORIGIN;
104
- return getRelativeLocation(absoluteLocation, container);
40
+ export namespace CreateOperationHandler {
41
+ export function is(object: unknown): object is CreateOperationHandler {
42
+ return (
43
+ object instanceof OperationHandler &&
44
+ hasStringProp(object, 'label') &&
45
+ hasArrayProp(object, 'elementTypeIds') &&
46
+ hasFunctionProp(object, 'getTriggerActions')
47
+ );
105
48
  }
106
49
  }
@@ -13,57 +13,81 @@
13
13
  *
14
14
  * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
15
  ********************************************************************************/
16
- import { Action, CreateOperation, MaybePromise, Operation, ServerMessageAction } from '@eclipse-glsp/protocol';
16
+ import { Action, MaybePromise, Operation, ServerMessageAction } from '@eclipse-glsp/protocol';
17
17
  import { inject, injectable } from 'inversify';
18
18
  import { ActionHandler } from '../actions/action-handler';
19
+ import { Command } from '../command/command';
20
+ import { CommandStack } from '../command/command-stack';
19
21
  import { Operations } from '../di/service-identifiers';
20
22
  import { ModelState } from '../features/model/model-state';
21
23
  import { ModelSubmissionHandler } from '../features/model/model-submission-handler';
24
+ import { GLSPServerError } from '../utils/glsp-server-error';
22
25
  import { OperationHandler } from './operation-handler';
23
26
  import { OperationHandlerRegistry } from './operation-handler-registry';
24
27
 
25
28
  @injectable()
26
29
  export class OperationActionHandler implements ActionHandler {
27
- @inject(OperationHandlerRegistry) protected operationHandlerRegistry: OperationHandlerRegistry;
28
- @inject(ModelSubmissionHandler) protected modelSubmissionHandler: ModelSubmissionHandler;
29
- @inject(ModelState) protected modelState: ModelState;
30
+ @inject(OperationHandlerRegistry)
31
+ protected operationHandlerRegistry: OperationHandlerRegistry;
32
+
33
+ @inject(ModelSubmissionHandler)
34
+ protected modelSubmissionHandler: ModelSubmissionHandler;
35
+
36
+ @inject(ModelState)
37
+ protected modelState: ModelState;
38
+
39
+ @inject(CommandStack)
40
+ protected commandStack: CommandStack;
30
41
 
31
42
  constructor(@inject(Operations) readonly actionKinds: string[]) {}
32
43
 
33
44
  execute(action: Operation): MaybePromise<Action[]> {
34
- if (this.handles(action)) {
35
- if (this.modelState.isReadonly) {
36
- return [
37
- ServerMessageAction.create(`Server is in readonly-mode! Could not execute operation: ${action.kind}`, {
38
- severity: 'WARNING'
39
- })
40
- ];
41
- }
42
- const operationHandler = OperationActionHandler.getOperationHandler(action, this.operationHandlerRegistry);
43
- if (operationHandler) {
44
- return this.executeHandler(action, operationHandler);
45
- }
45
+ if (!this.handles(action)) {
46
+ throw new GLSPServerError(`Unhandled operation kind: ${action.kind}`);
47
+ }
48
+ if (this.modelState.isReadonly) {
49
+ return [
50
+ ServerMessageAction.create(`Server is in readonly-mode! Could not execute operation: ${action.kind}`, {
51
+ severity: 'WARNING'
52
+ })
53
+ ];
54
+ }
55
+ return this.executeOperation(action);
56
+ }
57
+
58
+ protected executeOperation(operation: Operation): MaybePromise<Action[]> {
59
+ const operationHandler = OperationActionHandler.getOperationHandler(operation, this.operationHandlerRegistry);
60
+ if (operationHandler) {
61
+ return this.executeHandler(operation, operationHandler);
46
62
  }
47
63
  return [];
48
64
  }
49
65
 
50
- async executeHandler(operation: Operation, handler: OperationHandler): Promise<Action[]> {
51
- // TODO: Create GModelRecordingCommand;
52
- await handler.execute(operation);
53
- this.modelState.index.indexRoot(this.modelState.root);
54
- this.modelState.isDirty = true;
55
- // TODO: this.modelState.execute(command)
66
+ protected async executeHandler(operation: Operation, handler: OperationHandler): Promise<Action[]> {
67
+ const command = await handler.execute(operation);
68
+ if (command) {
69
+ this.executeCommand(command);
70
+ }
71
+ return this.modelSubmissionHandler.submitModel('operation');
72
+ }
73
+
74
+ protected executeCommand(command: Command): void {
75
+ this.commandStack.execute(command);
76
+ }
77
+
78
+ protected submitModel(): MaybePromise<Action[]> {
56
79
  return this.modelSubmissionHandler.submitModel('operation');
57
80
  }
58
81
 
59
82
  handles(action: Action): boolean {
60
- if (this.actionKinds.includes(action.kind)) {
61
- return true;
62
- }
63
- return false;
83
+ return this.actionKinds.includes(action.kind);
64
84
  }
65
85
 
86
+ /**
87
+ * @Deprecated Use {@link OperationHandlerRegistry#getOperationHandler(Operation) instead}.
88
+ */
89
+
66
90
  static getOperationHandler(operation: Operation, registry: OperationHandlerRegistry): OperationHandler | undefined {
67
- return CreateOperation.is(operation) ? registry.get(`${operation.kind}_${operation.elementTypeId}`) : registry.get(operation.kind);
91
+ return registry.getOperationHandler(operation);
68
92
  }
69
93
  }
@@ -13,8 +13,9 @@
13
13
  *
14
14
  * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
15
  ********************************************************************************/
16
- import { Args } from '@eclipse-glsp/protocol';
16
+ import { Args, CreateOperation, MaybePromise, Operation } from '@eclipse-glsp/protocol';
17
17
  import { inject, injectable, optional } from 'inversify';
18
+ import { Command } from '../command/command';
18
19
  import { ClientSessionInitializer } from '../session/client-session-initializer';
19
20
  import { Registry } from '../utils/registry';
20
21
  import { CreateOperationHandler } from './create-operation-handler';
@@ -23,13 +24,27 @@ import { OperationHandler, OperationHandlerConstructor, OperationHandlerFactory
23
24
  @injectable()
24
25
  export class OperationHandlerRegistry extends Registry<string, OperationHandler> {
25
26
  registerHandler(handler: OperationHandler): boolean {
26
- if (handler instanceof CreateOperationHandler) {
27
+ if (CreateOperationHandler.is(handler)) {
27
28
  handler.elementTypeIds.forEach(typeId => this.register(`${handler.operationType}_${typeId}`, handler));
28
29
  return true;
29
30
  } else {
30
31
  return this.register(handler.operationType, handler);
31
32
  }
32
33
  }
34
+
35
+ getOperationHandler(operation: Operation): OperationHandler | undefined {
36
+ return CreateOperation.is(operation) ? this.get(`${operation.kind}_${operation.elementTypeId}`) : this.get(operation.kind);
37
+ }
38
+
39
+ /**
40
+ * Returns the matching command for the given operation.
41
+ *
42
+ * @param operation operation
43
+ * @return the matching command for the given operation
44
+ */
45
+ getExecutableCommand(operation: Operation): MaybePromise<Command | undefined> {
46
+ return this.getOperationHandler(operation)?.createCommand(operation);
47
+ }
33
48
  }
34
49
 
35
50
  @injectable()
@@ -44,7 +59,7 @@ export class OperationHandlerRegistryInitializer implements ClientSessionInitial
44
59
  @inject(OperationHandlerRegistry)
45
60
  protected registry: OperationHandlerRegistry;
46
61
 
47
- initialize(args?: Args): void {
62
+ initialize(_args?: Args): void {
48
63
  const handlers = this.handlerConstructors.map(constructor => this.factory(constructor));
49
64
  handlers.forEach(handler => this.registry.registerHandler(handler));
50
65
  }
@@ -14,38 +14,63 @@
14
14
  * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
15
  ********************************************************************************/
16
16
  import { MaybePromise, Operation } from '@eclipse-glsp/protocol';
17
- import { interfaces } from 'inversify';
18
-
19
- export const OperationHandler = Symbol('OperationHandler');
17
+ import { inject, injectable, interfaces } from 'inversify';
18
+ import { Command } from '../command/command';
19
+ import { ModelState } from '../features/model/model-state';
20
20
 
21
21
  /**
22
22
  * An operation handler can execute {@link Operation}s of a certain type (subclass).
23
23
  * The operation handler processes the operation in the {@link OperationHandler.execute()} method. The result
24
- * of the execution is an update of the `ModelState` state.
25
- * This update is reversible (undo) and can be reapplied (redo). For basic diagram languages these updates are typically
26
- * applied directly on the `ModelState` using EMF `Command`s and the
27
- * `ModelState.execute()` method. For more complex diagram languages the
28
- * GModel state might be updated indirectly and the operation handler manipulates a custom model representation.
24
+ * of the execution is a {@link Command} that captures the corresponding source model changes.
25
+ * This command can be applied on the `CommandStack` and is reversible (undo) and can be reapplied (redo).
29
26
  *
30
27
  * The `OperationActionHandler` is responsible for retrieving all available (valid) operation handlers for an
31
28
  * operation that is dispatched via `ActionDispatcher`.
32
29
  */
33
- export interface OperationHandler {
30
+ @injectable()
31
+ export abstract class OperationHandler {
32
+ @inject(ModelState)
33
+ protected modelState: ModelState;
34
+
34
35
  /**
35
36
  * Returns the operation type that can be handled by this operation handler.
36
37
  *
37
38
  * @returns the operation type that can be handled.
38
39
  */
39
- readonly operationType: string;
40
+ abstract operationType: string;
40
41
 
41
42
  readonly label?: string;
42
43
 
43
44
  /**
44
- * Executes the operation handler for the given {@link Operation}.
45
+ * Creates a command that performs the operation in the source model(s). If `undefined` is returned, no update
46
+ * is performed on the model(s).
47
+ *
48
+ * @param operation The operation to process.
49
+ * @return The created command to be executed on the command stack or `undefined` nothing should be done.
50
+ */
51
+ abstract createCommand(operation: Operation): MaybePromise<Command | undefined>;
52
+
53
+ /**
54
+ * Executes the operation handler for the given {@link Operation} and returns the corresponding {@link Command}.
55
+ * If the given operation cannot be handled by this handler or the handler execution did not result in any changes
56
+ * `undefined` is returned.
57
+ *
58
+ * @param operation The operation that should be executed or empty if nothing should be done.
59
+ * @returns The command capturing the execution changes or `undefined`
60
+ */
61
+ execute(operation: Operation): MaybePromise<Command | undefined> {
62
+ return this.handles(operation) ? this.createCommand(operation) : undefined;
63
+ }
64
+
65
+ /**
66
+ * Validates whether the given {@link Operation} can be handled by this operation handler.
45
67
  *
46
- * @param operation The operation that should be executed.
68
+ * @param operation The operation that should be validated.
69
+ * @returns `true` if the given operation can be handled, `false` otherwise.
47
70
  */
48
- execute(operation: Operation): MaybePromise<void>;
71
+ handles(operation: Operation): boolean {
72
+ return this.modelState.root && operation.kind === this.operationType;
73
+ }
49
74
  }
50
75
 
51
76
  export const OperationHandlerConstructor = Symbol('OperationHandlerConstructor');
@@ -13,6 +13,7 @@
13
13
  *
14
14
  * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
15
  ********************************************************************************/
16
+ /* eslint-disable @typescript-eslint/no-empty-function */
16
17
  // Stub-implementation classes used for unit testing
17
18
 
18
19
  import { GEdge, GModelElement, GModelElementConstructor, GNode } from '@eclipse-glsp/graph';
@@ -33,6 +34,7 @@ import { Container } from 'inversify';
33
34
  import { MessageConnection } from 'vscode-jsonrpc';
34
35
  import { ActionDispatcher } from '../actions/action-dispatcher';
35
36
  import { ActionHandler, ActionHandlerFactory } from '../actions/action-handler';
37
+ import { Command } from '../command/command';
36
38
  import { DiagramConfiguration, ServerLayoutKind } from '../diagram/diagram-configuration';
37
39
  import { ContextEditValidator } from '../features/directediting/context-edit-validator';
38
40
  import { LabelEditValidator } from '../features/directediting/label-edit-validator';
@@ -103,9 +105,7 @@ export class StubCreateEdgeOperationHandler extends GModelCreateEdgeOperationHan
103
105
  }
104
106
 
105
107
  export class StubActionDispatcher implements ActionDispatcher {
106
- dispatchAfterNextUpdate(...actions: MaybeArray<Action[]>): void {
107
- // NO-OP
108
- }
108
+ dispatchAfterNextUpdate(...actions: MaybeArray<Action[]>): void {}
109
109
 
110
110
  dispatch(action: Action): Promise<void> {
111
111
  return Promise.resolve();
@@ -158,51 +158,31 @@ export class StubLogger extends Logger {
158
158
  logLevel = LogLevel.none;
159
159
  caller = undefined;
160
160
 
161
- info(message: string, ...params: any[]): void {
162
- // no-op
163
- }
161
+ info(message: string, ...params: any[]): void {}
164
162
 
165
- warn(message: string, ...params: any[]): void {
166
- // no-op
167
- }
163
+ warn(message: string, ...params: any[]): void {}
168
164
 
169
- error(message: string, ...params: any[]): void {
170
- // no-op
171
- }
165
+ error(message: string, ...params: any[]): void {}
172
166
 
173
- debug(message: string, ...params: any[]): void {
174
- // no-op
175
- }
167
+ debug(message: string, ...params: any[]): void {}
176
168
  }
177
169
 
178
170
  export class StubClientSessionListener implements ClientSessionListener {
179
- sessionCreated(clientSession: ClientSession): void {
180
- // no-op
181
- }
171
+ sessionCreated(clientSession: ClientSession): void {}
182
172
 
183
- sessionDisposed(clientSession: ClientSession): void {
184
- // no-op
185
- }
173
+ sessionDisposed(clientSession: ClientSession): void {}
186
174
  }
187
175
 
188
176
  export class StubGLSPClientProxy implements JsonRpcGLSPClientProxy {
189
- connect(connection: MessageConnection): void {
190
- // no-op
191
- }
177
+ connect(connection: MessageConnection): void {}
192
178
 
193
- process(message: ActionMessage<Action>): void {
194
- // no-op
195
- }
179
+ process(message: ActionMessage<Action>): void {}
196
180
  }
197
181
 
198
182
  export class StubGLSPServerListener implements GLSPServerListener {
199
- serverInitialized(server: GLSPServer): void {
200
- // no-op
201
- }
183
+ serverInitialized(server: GLSPServer): void {}
202
184
 
203
- serverShutDown(server: GLSPServer): void {
204
- // no-op
205
- }
185
+ serverShutDown(server: GLSPServer): void {}
206
186
  }
207
187
 
208
188
  export class StubDiagramConfiguration implements DiagramConfiguration {
@@ -250,7 +230,12 @@ export class TestContextEditValidator implements ContextEditValidator {
250
230
  export const stubActionHandlerFactory: ActionHandlerFactory = constructor => new constructor();
251
231
 
252
232
  export class StubClientSessionInitializer implements ClientSessionInitializer {
253
- initialize(args?: Args): void {
254
- // no-op
255
- }
233
+ initialize(args?: Args): void {}
234
+ }
235
+
236
+ export class StubCommand implements Command {
237
+ execute(): void {}
238
+ undo(): void {}
239
+ redo(): void {}
240
+ canUndo?(): boolean;
256
241
  }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=apply-label-edit-operation-handler.spec.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"apply-label-edit-operation-handler.spec.d.ts","sourceRoot":"","sources":["../../../src/common/gmodel/apply-label-edit-operation-handler.spec.ts"],"names":[],"mappings":""}
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- /********************************************************************************
4
- * Copyright (c) 2022-2023 STMicroelectronics and others.
5
- *
6
- * This program and the accompanying materials are made available under the
7
- * terms of the Eclipse Public License v. 2.0 which is available at
8
- * http://www.eclipse.org/legal/epl-2.0.
9
- *
10
- * This Source Code may also be made available under the following Secondary
11
- * Licenses when the conditions for such availability set forth in the Eclipse
12
- * Public License v. 2.0 are satisfied: GNU General Public License, version 2
13
- * with the GNU Classpath Exception which is available at
14
- * https://www.gnu.org/software/classpath/license.html.
15
- *
16
- * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
17
- ********************************************************************************/
18
- const graph_1 = require("@eclipse-glsp/graph");
19
- const protocol_1 = require("@eclipse-glsp/protocol");
20
- const chai_1 = require("chai");
21
- const sinon = require("sinon");
22
- const gmodel_index_1 = require("../features/model/gmodel-index");
23
- const model_state_1 = require("../features/model/model-state");
24
- const apply_label_edit_operation_handler_1 = require("./apply-label-edit-operation-handler");
25
- const assert = require("assert");
26
- describe('Test ApplyLabelEditOperationHandler', () => {
27
- const label = new graph_1.GLabel();
28
- const modelState = new model_state_1.DefaultModelState();
29
- Object.defineProperty(modelState, 'index', { value: new gmodel_index_1.GModelIndex() });
30
- sinon.stub(modelState.index, 'findByClass').returns(label);
31
- const applyLabelEditOperationHandler = new apply_label_edit_operation_handler_1.ApplyLabelEditOperationHandler();
32
- Object.defineProperty(applyLabelEditOperationHandler, 'modelState', { value: modelState });
33
- it('text is changed after ApplyLabelEditOperation', async () => {
34
- assert.doesNotThrow(() => applyLabelEditOperationHandler.execute(protocol_1.ApplyLabelEditOperation.create({ labelId: 'myId', text: 'test' })));
35
- (0, chai_1.expect)(label.text).to.be.equal('test');
36
- });
37
- });
38
- //# sourceMappingURL=apply-label-edit-operation-handler.spec.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"apply-label-edit-operation-handler.spec.js","sourceRoot":"","sources":["../../../src/common/gmodel/apply-label-edit-operation-handler.spec.ts"],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;kFAckF;AAClF,+CAA6C;AAC7C,qDAAiE;AACjE,+BAA8B;AAC9B,+BAA+B;AAC/B,iEAA6D;AAC7D,+DAAkE;AAClE,6FAAsF;AAEtF,iCAAkC;AAElC,QAAQ,CAAC,qCAAqC,EAAE,GAAG,EAAE;IACjD,MAAM,KAAK,GAAG,IAAI,cAAM,EAAE,CAAC;IAC3B,MAAM,UAAU,GAAG,IAAI,+BAAiB,EAAE,CAAC;IAC3C,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,IAAI,0BAAW,EAAE,EAAE,CAAC,CAAC;IACzE,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC3D,MAAM,8BAA8B,GAAG,IAAI,mEAA8B,EAAE,CAAC;IAC5E,MAAM,CAAC,cAAc,CAAC,8BAA8B,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,UAAU,EAAE,CAAC,CAAC;IAE3F,EAAE,CAAC,+CAA+C,EAAE,KAAK,IAAI,EAAE;QAC3D,MAAM,CAAC,YAAY,CAAC,GAAG,EAAE,CACrB,8BAA8B,CAAC,OAAO,CAAC,kCAAuB,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC,CAC5G,CAAC;QACF,IAAA,aAAM,EAAC,KAAK,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAC3C,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"computed-bounds-action-handler.d.ts","sourceRoot":"","sources":["../../../src/common/gmodel/computed-bounds-action-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;kFAckF;AAClF,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEpF,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAC3D,OAAO,EAAE,sBAAsB,EAAE,MAAM,4CAA4C,CAAC;AAGpF,qBACa,2BAA4B,YAAW,aAAa;IAE7D,SAAS,CAAC,iBAAiB,EAAE,sBAAsB,CAAC;IAGpD,SAAS,CAAC,UAAU,EAAE,UAAU,CAAC;IAEjC,WAAW,WAA+B;IAC1C,OAAO,CAAC,MAAM,EAAE,oBAAoB,GAAG,YAAY,CAAC,MAAM,EAAE,CAAC;IAU7D,SAAS,CAAC,WAAW,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,oBAAoB,GAAG,IAAI;IAO3E,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACjC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"computed-bounds-action-handler.js","sourceRoot":"","sources":["../../../src/common/gmodel/computed-bounds-action-handler.ts"],"names":[],"mappings":";;;;;;;;;;;;AAgBA,qDAAoF;AACpF,yCAA+C;AAE/C,+DAA2D;AAC3D,yFAAoF;AACpF,sDAA+E;AAGxE,IAAM,2BAA2B,GAAjC,MAAM,2BAA2B;IAAjC;QAOH,gBAAW,GAAG,CAAC,+BAAoB,CAAC,IAAI,CAAC,CAAC;IAmB9C,CAAC;IAlBG,OAAO,CAAC,MAA4B;QAChC,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;QACnC,IAAI,MAAM,CAAC,QAAQ,KAAK,KAAK,CAAC,QAAQ,EAAE;YACpC,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAChC,OAAO,IAAI,CAAC,iBAAiB,CAAC,mBAAmB,EAAE,CAAC;SACvD;QAED,OAAO,EAAE,CAAC;IACd,CAAC;IAES,WAAW,CAAC,IAAgB,EAAE,MAA4B;;QAChE,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC;QACpC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,IAAA,yBAAW,EAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;QAC5D,CAAC,MAAA,MAAM,CAAC,UAAU,mCAAI,EAAE,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC,IAAA,4BAAc,EAAC,SAAS,EAAE,KAAK,CAAC,CAAC,CAAC;QACjF,CAAC,MAAA,MAAM,CAAC,MAAM,mCAAI,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,IAAA,wBAAU,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;IACrE,CAAC;CAGJ,CAAA;AAzBG;IAAC,IAAA,kBAAM,EAAC,iDAAsB,CAAC;8BACF,iDAAsB;sEAAC;AAEpD;IAAC,IAAA,kBAAM,EAAC,wBAAU,CAAC;;+DACc;AALxB,2BAA2B;IADvC,IAAA,sBAAU,GAAE;GACA,2BAA2B,CA0BvC;AA1BY,kEAA2B"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"cut-operation-handler.d.ts","sourceRoot":"","sources":["../../../src/common/operations/cut-operation-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;kFAckF;AAClF,OAAO,EAAE,YAAY,EAA0B,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE5F,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD,qBACa,mBAAoB,YAAW,gBAAgB;IACxD,aAAa,SAAqB;IAGlC,SAAS,CAAC,gBAAgB,EAAE,gBAAgB,CAAC;IAE7C,OAAO,CAAC,SAAS,EAAE,YAAY,GAAG,YAAY,CAAC,IAAI,CAAC;IAOpD,SAAS,CAAC,eAAe,CAAC,YAAY,EAAE,YAAY,GAAG,MAAM,EAAE;CAGlE"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"cut-operation-handler.js","sourceRoot":"","sources":["../../../src/common/operations/cut-operation-handler.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA;;;;;;;;;;;;;;kFAckF;AAClF,qDAA4F;AAC5F,yCAA+C;AAC/C,oEAAgE;AAIzD,IAAM,mBAAmB,GAAzB,MAAM,mBAAmB;IAAzB;QACH,kBAAa,GAAG,uBAAY,CAAC,IAAI,CAAC;IAetC,CAAC;IAVG,OAAO,CAAC,SAAuB;QAC3B,MAAM,iBAAiB,GAAG,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC;QAC1D,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE;YAC9B,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,iCAAsB,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC,CAAC;SACpF;IACL,CAAC;IAES,eAAe,CAAC,YAA0B;QAChD,OAAO,YAAY,CAAC,aAAa,CAAC,kBAAkB,CAAC;IACzD,CAAC;CACJ,CAAA;AAbG;IAAC,IAAA,kBAAM,EAAC,oCAAgB,CAAC;;6DACoB;AAJpC,mBAAmB;IAD/B,IAAA,sBAAU,GAAE;GACA,mBAAmB,CAgB/B;AAhBY,kDAAmB"}
@@ -1,40 +0,0 @@
1
- /********************************************************************************
2
- * Copyright (c) 2022-2023 STMicroelectronics and others.
3
- *
4
- * This program and the accompanying materials are made available under the
5
- * terms of the Eclipse Public License v. 2.0 which is available at
6
- * http://www.eclipse.org/legal/epl-2.0.
7
- *
8
- * This Source Code may also be made available under the following Secondary
9
- * Licenses when the conditions for such availability set forth in the Eclipse
10
- * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- * with the GNU Classpath Exception which is available at
12
- * https://www.gnu.org/software/classpath/license.html.
13
- *
14
- * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
- ********************************************************************************/
16
- import { GLabel } from '@eclipse-glsp/graph';
17
- import { ApplyLabelEditOperation } from '@eclipse-glsp/protocol';
18
- import { expect } from 'chai';
19
- import * as sinon from 'sinon';
20
- import { GModelIndex } from '../features/model/gmodel-index';
21
- import { DefaultModelState } from '../features/model/model-state';
22
- import { ApplyLabelEditOperationHandler } from './apply-label-edit-operation-handler';
23
-
24
- import assert = require('assert');
25
-
26
- describe('Test ApplyLabelEditOperationHandler', () => {
27
- const label = new GLabel();
28
- const modelState = new DefaultModelState();
29
- Object.defineProperty(modelState, 'index', { value: new GModelIndex() });
30
- sinon.stub(modelState.index, 'findByClass').returns(label);
31
- const applyLabelEditOperationHandler = new ApplyLabelEditOperationHandler();
32
- Object.defineProperty(applyLabelEditOperationHandler, 'modelState', { value: modelState });
33
-
34
- it('text is changed after ApplyLabelEditOperation', async () => {
35
- assert.doesNotThrow(() =>
36
- applyLabelEditOperationHandler.execute(ApplyLabelEditOperation.create({ labelId: 'myId', text: 'test' }))
37
- );
38
- expect(label.text).to.be.equal('test');
39
- });
40
- });