Skip to content

Commit

Permalink
Make comment retrieval pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
Lotes committed Jul 10, 2023
1 parent e4a93c8 commit df3856a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/langium/src/documentation/comment-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
******************************************************************************/

import type { GrammarConfig } from '../grammar';
import type { AstNodeWithComment } from '../serializer';
import { isAstNodeWithComment } from '../serializer';
import type { LangiumServices } from '../services';
import type { AstNode } from '../syntax-tree';
import { findCommentNode } from '../utils/cst-util';
Expand All @@ -28,8 +28,8 @@ export class DefaultCommentProvider implements CommentProvider {
this.grammarConfig = () => services.parser.GrammarConfig;
}
getComment(node: AstNode): string | undefined {
if('$comment' in node) {
return (node as AstNodeWithComment).$comment;
if(isAstNodeWithComment(node)) {
return node.$comment;
}
return findCommentNode(node.$cstNode, this.grammarConfig().multilineCommentRules)?.text;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/langium/src/serializer/json-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export interface AstNodeWithComment extends AstNode {
$comment?: string;
}

export function isAstNodeWithComment(node: AstNode): node is AstNodeWithComment {
return typeof (node as AstNodeWithComment).$comment === 'string';
}

/**
* A {@DocumentSegment} representing the definition area of an AstNode within the DSL text.
* Usually contains text region information on all assigned property values of the AstNode,
Expand Down

0 comments on commit df3856a

Please sign in to comment.