Skip to content
Snippets Groups Projects
Commit 5ebb4298 authored by Miklos Magyari's avatar Miklos Magyari
Browse files

Implemented document comment generation


Signed-off-by: default avatarMiklos Magyari <miklos.magyari@sigmatechnology.com>
parent 7fb9da1c
No related branches found
No related tags found
1 merge request!72Implemented document comment generation
This commit is part of merge request !72. Comments created here will be created in the context of that merge request.
......@@ -14,6 +14,7 @@ import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.SymbolKind;
import org.eclipse.titan.lsp.AST.ASTVisitor;
import org.eclipse.titan.lsp.AST.AstSemanticHighlighting;
import org.eclipse.titan.lsp.AST.DocumentComment;
import org.eclipse.titan.lsp.AST.IReferenceChain;
import org.eclipse.titan.lsp.AST.ISubReference;
......@@ -31,6 +32,7 @@ import org.eclipse.titan.lsp.AST.ReferenceFinder;
import org.eclipse.titan.lsp.AST.ReferenceFinder.Hit;
import org.eclipse.titan.lsp.AST.Scope;
import org.eclipse.titan.lsp.AST.Type;
import org.eclipse.titan.lsp.AST.AstSemanticHighlighting.SemanticType;
import org.eclipse.titan.lsp.AST.TTCN3.attributes.AttributeSpecification;
import org.eclipse.titan.lsp.AST.TTCN3.attributes.ExtensionAttribute;
import org.eclipse.titan.lsp.AST.TTCN3.attributes.MultipleWithAttributes;
......@@ -43,7 +45,10 @@ import org.eclipse.titan.lsp.AST.TTCN3.types.Address_Type;
import org.eclipse.titan.lsp.AST.TTCN3.types.Altstep_Type;
import org.eclipse.titan.lsp.AST.TTCN3.types.Class_Type;
import org.eclipse.titan.lsp.AST.TTCN3.types.Component_Type;
import org.eclipse.titan.lsp.AST.TTCN3.types.EnumItem;
import org.eclipse.titan.lsp.AST.TTCN3.types.Function_Type;
import org.eclipse.titan.lsp.AST.TTCN3.types.TTCN3_Enumerated_Type;
import org.eclipse.titan.lsp.AST.TTCN3.types.TTCN3_Set_Seq_Choice_BaseType;
import org.eclipse.titan.lsp.AST.TTCN3.types.Testcase_Type;
import org.eclipse.titan.lsp.hover.HoverContentType;
import org.eclipse.titan.lsp.hover.Ttcn3HoverContent;
......@@ -270,7 +275,7 @@ public final class Def_Type extends Definition implements ICommentable {
default:
break;
}
// AstSyntaxHighlightTokens.addSyntaxDecoration(identifier.getLocation(), SyntaxDecoration.DefType);
//AstSemanticHighlighting.addSemanticToken(location, SemanticType.Type);
checkDocumentComment();
}
......@@ -640,72 +645,72 @@ public final class Def_Type extends Definition implements ICommentable {
return hoverContent;
}
//
// @Override
// public String generateDocComment(final String indentation) {
// final CompilationTimeStamp timestamp = lastTimeChecked != null ? lastTimeChecked : CompilationTimeStamp.getBaseTimestamp();
// if (timestamp == null) {
// return "";
// }
// final IType assType = getType(timestamp);
//
// final String ind = indentation + ICommentable.LINE_START;
// final StringBuilder sb = new StringBuilder();
// sb.append(ICommentable.COMMENT_START)
// .append(ind).append(ICommentable.DESC_TAG).append('\n');
//
// switch (assType.getTypetype()) {
// case TYPE_CLASS:
// final Class_Type ct = (Class_Type)assType;
// for (final Definition def : ct.getDefinitions()) {
// boolean functionOverride = false;
// if (def instanceof Def_Function) {
// if (((Def_Function)def).isOverride()) {
// functionOverride = true;
// }
// }
// if (! def.isInherited() || functionOverride) {
// sb.append(ind).append(ICommentable.MEMBER_TAG).append(' ')
// .append(def.getIdentifier().getDisplayName())
// .append('\n');
// }
// }
// break;
// case TYPE_TTCN3_CHOICE:
// case TYPE_TTCN3_SEQUENCE:
// final TTCN3_Set_Seq_Choice_BaseType choice = (TTCN3_Set_Seq_Choice_BaseType)assType;
// for (int i = 0; i < choice.getNofComponents(); i++) {
// sb.append(ind).append(ICommentable.MEMBER_TAG).append(' ')
// .append(choice.getComponentByIndex(i).getIdentifier().getDisplayName())
// .append('\n');
// }
// break;
// case TYPE_COMPONENT:
// final Component_Type component = (Component_Type)assType;
// for (final Definition def : component.getComponentBody().getDefinitions()) {
// sb.append(ind).append(ICommentable.MEMBER_TAG).append(' ')
// .append(def.getIdentifier().getDisplayName())
// .append('\n');
// }
// break;
// case TYPE_TTCN3_ENUMERATED:
// final TTCN3_Enumerated_Type enumerated = (TTCN3_Enumerated_Type)assType;
// for (final EnumItem item : enumerated.getEnumItemsOrdered()) {
// sb.append(ind).append(ICommentable.MEMBER_TAG).append(' ')
// .append(item.getId().getDisplayName())
// .append('\n');
// }
// break;
// default:
// break;
// }
//
// sb.append(indentation).append(ICommentable.COMMENT_END).append(indentation);
// return sb.toString();
// }
@Override
public String generateDocComment(String indentation) {
// TODO Auto-generated method stub
return null;
public String generateDocComment(final String indentation) {
final CompilationTimeStamp timestamp = lastTimeChecked != null ? lastTimeChecked : CompilationTimeStamp.getBaseTimestamp();
if (timestamp == null) {
return "";
}
final IType assType = getType(timestamp);
final String ind = indentation + ICommentable.LINE_START;
final StringBuilder sb = new StringBuilder();
sb.append(ICommentable.COMMENT_START)
.append(ind).append(ICommentable.DESC_TAG).append('\n');
switch (assType.getTypetype()) {
case TYPE_CLASS:
final Class_Type ct = (Class_Type)assType;
for (final Definition def : ct.getDefinitions()) {
boolean functionOverride = false;
if (def instanceof Def_Function) {
if (((Def_Function)def).isOverride()) {
functionOverride = true;
}
}
if (! def.isInherited() || functionOverride) {
sb.append(ind).append(ICommentable.MEMBER_TAG).append(' ')
.append(def.getIdentifier().getDisplayName())
.append('\n');
}
}
break;
case TYPE_TTCN3_CHOICE:
case TYPE_TTCN3_SEQUENCE:
final TTCN3_Set_Seq_Choice_BaseType choice = (TTCN3_Set_Seq_Choice_BaseType)assType;
for (int i = 0; i < choice.getNofComponents(); i++) {
sb.append(ind).append(ICommentable.MEMBER_TAG).append(' ')
.append(choice.getComponentByIndex(i).getIdentifier().getDisplayName())
.append('\n');
}
break;
case TYPE_COMPONENT:
final Component_Type component = (Component_Type)assType;
for (final Definition def : component.getComponentBody().getDefinitions()) {
sb.append(ind).append(ICommentable.MEMBER_TAG).append(' ')
.append(def.getIdentifier().getDisplayName())
.append('\n');
}
break;
case TYPE_TTCN3_ENUMERATED:
final TTCN3_Enumerated_Type enumerated = (TTCN3_Enumerated_Type)assType;
for (final EnumItem item : enumerated.getEnumItemsOrdered()) {
sb.append(ind).append(ICommentable.MEMBER_TAG).append(' ')
.append(item.getId().getDisplayName())
.append('\n');
}
break;
default:
break;
}
sb.append(indentation).append(ICommentable.COMMENT_END).append(indentation);
return sb.toString();
}
// @Override
// public String generateDocComment(String indentation) {
// // TODO Auto-generated method stub
// return null;
// }
}
......@@ -14,6 +14,7 @@ import org.eclipse.lsp4j.jsonrpc.services.JsonRequest;
import org.eclipse.lsp4j.jsonrpc.services.JsonSegment;
import org.eclipse.titan.lsp.lspextensions.DocumentCommentParams;
import org.eclipse.titan.lsp.lspextensions.ExcludedStatesChangedParams;
import org.eclipse.titan.lsp.lspextensions.GenerateDocumentCommentParams;
import org.eclipse.titan.lsp.lspextensions.OpenTpdParams;
import org.eclipse.titan.lsp.lspextensions.TpdProjectFiles;
......@@ -73,4 +74,9 @@ public interface LspExtensionsService {
default CompletableFuture<TpdProjectFiles> openTpd(OpenTpdParams params) {
throw new UnsupportedOperationException();
}
@JsonRequest
default CompletableFuture<String> generateDocumentComment(GenerateDocumentCommentParams params) {
throw new UnsupportedOperationException();
}
}
......@@ -15,11 +15,15 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.eclipse.lsp4j.HoverParams;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.titan.lsp.AST.Location;
import org.eclipse.titan.lsp.AST.Module;
import org.eclipse.titan.lsp.AST.ASN1.definitions.ASN1Module;
import org.eclipse.titan.lsp.AST.TTCN3.definitions.ICommentable;
import org.eclipse.titan.lsp.AST.TTCN3.definitions.TTCN3Module;
import org.eclipse.titan.lsp.common.product.TpdImporter;
import org.eclipse.titan.lsp.core.Position;
import org.eclipse.titan.lsp.core.Project;
import org.eclipse.titan.lsp.core.ProjectItem;
import org.eclipse.titan.lsp.declarationsearch.Declaration;
......@@ -27,6 +31,7 @@ import org.eclipse.titan.lsp.declarationsearch.IdentifierFinderVisitor;
import org.eclipse.titan.lsp.hover.HoverContentType;
import org.eclipse.titan.lsp.lspextensions.DocumentCommentParams;
import org.eclipse.titan.lsp.lspextensions.ExcludedStatesChangedParams;
import org.eclipse.titan.lsp.lspextensions.GenerateDocumentCommentParams;
import org.eclipse.titan.lsp.lspextensions.OpenTpdParams;
import org.eclipse.titan.lsp.lspextensions.TpdProjectFiles;
......@@ -123,4 +128,40 @@ public class TitanLspExtensionsService implements LspExtensionsService {
return tpdFiles;
});
}
@Override
public CompletableFuture<String> generateDocumentComment(GenerateDocumentCommentParams params) {
return CompletableFuture.supplyAsync(() -> {
Path path = null;
try {
final String filename = URLDecoder.decode(params.getIdentifier().getUri());
path = Path.of(new URI(filename));
if (path == null) {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
final Project project = Project.INSTANCE;
final Module module = project.getModuleByPath(path);
if (module instanceof TTCN3Module || module instanceof ASN1Module) {
final Position position = new Position(params.getPosition());
final IdentifierFinderVisitor visitor = new IdentifierFinderVisitor(position.getLine(), position.getCharacter());
module.accept(visitor);
final Declaration declaration = visitor.getReferencedDeclaration();
if (declaration != null) {
ICommentable commentable = declaration.getCommentable();
final Location location = declaration.getLocation();
if (commentable == null || commentable.hasDocumentComment() ||
location == null) {
return null;
}
return commentable.generateDocComment(params.getIndentation());
}
}
return null;
});
}
}
/******************************************************************************
* Copyright (c) 2000-2023 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
******************************************************************************/
package org.eclipse.titan.lsp.lspextensions;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.generator.JsonRpcData;
/**
* @author Miklos Magyari
**/
@JsonRpcData
public class GenerateDocumentCommentParams {
private TextDocumentIdentifier identifier;
private Position position;
private String indentation;
public GenerateDocumentCommentParams(final TextDocumentIdentifier identifier, final Position position, final String indentation) {
this.identifier = identifier;
this.position = position;
this.indentation = indentation;
}
public TextDocumentIdentifier getIdentifier() {
return identifier;
}
public Position getPosition() {
return position;
}
public String getIndentation() {
return indentation;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment