Skip to content

Commit

Permalink
Improve User tag snippet completion
Browse files Browse the repository at this point in the history
Fixes redhat-developer#784

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Feb 2, 2023
1 parent 5a1b1cf commit 73024af
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 349 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ public BinaryUserTag(UserTagInfo tagInfo) {
super(tagInfo.getFileName());
this.uri = tagInfo.getUri();
this.content = tagInfo.getContent();
super.setBody(createBody());
}

@Override
public String getUri() {
return uri;
}

@Override
public String getContent() {
return content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
*******************************************************************************/
package com.redhat.qute.project.tags;

import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Path;

import com.redhat.qute.utils.IOUtils;

/**
* Source user tag.
*
Expand All @@ -26,6 +30,7 @@ public class SourceUserTag extends UserTag {
public SourceUserTag(String fileName, Path path) {
super(fileName);
this.path = path;
super.setBody(createBody());
}

@Override
Expand All @@ -41,4 +46,15 @@ public String getUri() {
public Path getPath() {
return path;
}

@Override
public String getContent() {
try (InputStream input = new FileInputStream(path.toFile())) {
return IOUtils.convertStreamToString(input);

} catch (Exception e) {

}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@
*******************************************************************************/
package com.redhat.qute.project.tags;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.redhat.qute.ls.commons.snippets.Snippet;
import com.redhat.qute.ls.commons.snippets.SnippetsBuilder;
import com.redhat.qute.parser.expression.ObjectPart;
import com.redhat.qute.parser.template.Template;
import com.redhat.qute.parser.template.TemplateParser;
import com.redhat.qute.services.snippets.QuteSnippetContext;
import com.redhat.qute.utils.UserTagUtils;

Expand All @@ -29,17 +36,52 @@ public abstract class UserTag extends Snippet {

private final String fileName;
private final String templateId;
private List<ObjectPart> parameters;

public UserTag(String fileName) {
String name = UserTagUtils.getUserTagName(fileName);
super.setLabel(name);
super.setPrefixes(Arrays.asList(name));
super.setBody(Arrays.asList("{#" + name + " /}$0"));
super.setContext(QuteSnippetContext.IN_TEXT);
this.fileName = fileName;
this.templateId = UserTagUtils.getTemplateId(name);
}

protected List<String> createBody() {
String name = getLabel();
List<String> body = new ArrayList<>();
List<ObjectPart> parameters = getParameters();
boolean hasNestedContent = false;
boolean hasIt = false;
for (ObjectPart objectPart : parameters) {
if (UserTagUtils.IT_OBJECT_PART_NAME.equals(objectPart.getPartName())) {
hasIt = true;
} else if (UserTagUtils.NESTED_CONTENT_OBJECT_PART_NAME.equals(objectPart.getPartName())) {
hasNestedContent = true;
}
}
int index = 1;
StringBuilder first = new StringBuilder("{#");
first.append(name);
if (hasIt) {
first.append(" ");
SnippetsBuilder.placeholders(index++, UserTagUtils.IT_OBJECT_PART_NAME, first);
first.append(" ");
}
if (hasNestedContent) {
first.append("}");
} else {
first.append("/}");
SnippetsBuilder.tabstops(0, first);
}
body.add(first.toString());
if (hasNestedContent) {
body.add("\t" + SnippetsBuilder.tabstops(index++));
body.add("{/" + name + "}");
}
return body;
}

/**
* Returns the user tag name.
*
Expand Down Expand Up @@ -73,4 +115,36 @@ public String getTemplateId() {
* @return the Qute template file Uri.
*/
public abstract String getUri();

/**
* Returns the content of the user tag.
*
* @return the content of the user tag.
*/
public abstract String getContent();

public List<ObjectPart> getParameters() {
if (parameters == null) {
parameters = collectParameters();
}
return parameters;
}

private List<ObjectPart> collectParameters() {
Template template = getTemplate();
if (template == null) {
return Collections.emptyList();
}
UserTagParameterCollector collector = new UserTagParameterCollector();
template.accept(collector);
return collector.getParameters();
}

private Template getTemplate() {
String content = getContent();
if (content == null) {
return null;
}
return TemplateParser.parse(content, getFileName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.redhat.qute.project.tags;

import java.util.ArrayList;
import java.util.List;

import com.redhat.qute.parser.expression.ObjectPart;
import com.redhat.qute.parser.template.ASTVisitor;

public class UserTagParameterCollector extends ASTVisitor {

private final List<ObjectPart> parameters;

public UserTagParameterCollector() {
parameters = new ArrayList<>();
}

@Override
public boolean visit(ObjectPart node) {
parameters.add(node);
return super.visit(node);
}

public List<ObjectPart> getParameters() {
return parameters;
}
}
Loading

0 comments on commit 73024af

Please sign in to comment.