Skip to content

Commit

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

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr authored and datho7561 committed Feb 10, 2023
1 parent 5a1b1cf commit 65ec61d
Show file tree
Hide file tree
Showing 33 changed files with 942 additions and 441 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
import org.eclipse.lsp4j.DidCloseTextDocumentParams;
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextDocumentItem;

Expand All @@ -29,7 +30,7 @@ public class TextDocuments<T extends TextDocument> {

private final Map<String, T> documents;

private boolean incremental = true; //default on
private boolean incremental = true; // default on

public TextDocuments() {
documents = new HashMap<>();
Expand Down Expand Up @@ -108,6 +109,12 @@ public T onDidCloseTextDocument(DidCloseTextDocumentParams params) {
}
}

public T onDidSaveTextDocument(DidSaveTextDocumentParams params) {
synchronized (documents) {
return getDocument(params.getTextDocument());
}
}

private T getDocument(TextDocumentIdentifier identifier) {
return documents.get(identifier.getUri());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.eclipse.lsp4j.DidCloseTextDocumentParams;
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
import org.eclipse.lsp4j.TextDocumentItem;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;

Expand Down Expand Up @@ -58,4 +59,10 @@ public ModelTextDocument<Template> onDidCloseTextDocument(DidCloseTextDocumentPa
projectRegistry.onDidCloseTextDocument(document);
return document;
}

public ModelTextDocument<Template> onDidSaveTextDocument(DidSaveTextDocumentParams params) {
QuteTextDocument document = (QuteTextDocument) super.onDidSaveTextDocument(params);
projectRegistry.onDidSaveTextDocument(document);
return document;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void didClose(DidCloseTextDocumentParams params) {

@Override
public void didSave(DidSaveTextDocumentParams params) {

documents.onDidSaveTextDocument(params);
}

@Override
Expand Down Expand Up @@ -161,7 +161,8 @@ public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActio
// Cancel checker is not passed to doCodeActions, since code actions don't yet
// need to interact with JDT/editor
return getQuteLanguageService()
.doCodeActions(template, params.getContext(), getLanguageClient(), params.getRange(), sharedSettings) //
.doCodeActions(template, params.getContext(), getLanguageClient(), params.getRange(),
sharedSettings) //
.thenApply(codeActions -> {
cancelChecker.checkCanceled();
return codeActions.stream() //
Expand Down Expand Up @@ -293,7 +294,7 @@ public CompletableFuture<List<InlayHint>> inlayHint(InlayHintParams params) {
return hints;
});
}

@Override
public CompletableFuture<CodeAction> resolveCodeAction(CodeAction codeAction) {
return getQuteLanguageService().resolveCodeAction(codeAction, getLanguageClient());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
*******************************************************************************/
package com.redhat.qute.parser.expression;

import java.util.List;

import com.redhat.qute.parser.expression.Parts.PartKind;
import com.redhat.qute.parser.template.ASTVisitor;
import com.redhat.qute.parser.template.Expression;
Expand Down Expand Up @@ -91,40 +89,35 @@ public JavaTypeInfoProvider resolveJavaType() {
Section section = super.getParentSection();
while (section != null) {
switch (section.getSectionKind()) {
case EACH:
case FOR:
LoopSection iterableSection = (LoopSection) section;
if (!iterableSection.isInElseBlock(getStart())) {
String alias = iterableSection.getAlias();
if (partName.equals(alias)) {
return iterableSection.getIterableParameter();
case EACH:
case FOR:
LoopSection iterableSection = (LoopSection) section;
if (!iterableSection.isInElseBlock(getStart())) {
String alias = iterableSection.getAlias();
if (partName.equals(alias)) {
return iterableSection.getIterableParameter();
}
}
}
break;
case LET:
case SET: {
List<Parameter> parameters = section.getParameters();
for (Parameter parameter : parameters) {
if (partName.equals(parameter.getName())) {
break;
case LET:
case SET: {
Parameter parameter = section.findParameter(partName);
if (parameter != null) {
return parameter;
}
break;
}
break;
}
case IF: {
if (matchedOptionalParameter == null) {
List<Parameter> parameters = section.getParameters();
for (Parameter parameter : parameters) {
if (partName.equals(parameter.getName()) && parameter.isOptional()) {
case IF: {
if (matchedOptionalParameter == null) {
Parameter parameter = section.findParameter(partName);
if (parameter != null && parameter.isOptional()) {
// here {foo} is inside an #if block which matches {#if foo?? }
matchedOptionalParameter = parameter;
}
break;
}
break;
}
break;
}
default:
default:
}
// ex : count for #each
JavaTypeInfoProvider metadata = section.getMetadata(partName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,37 @@ public Parameter getParameterAtOffset(int offset) {
offset);
}

/**
* Returns the parameter which have the given name <code>parameterName</code>
* and null otherwise.
*
* @param parameterName the parameter name.
*
* @return the parameter which have the given name <code>parameterName</code>
* and null otherwise.
*/
public Parameter findParameter(String parameterName) {
for (Parameter parameter : getParameters()) {
if (parameterName.equals(parameter.getName())) {
return parameter;
}
}
return null;
}

/**
* Returns true if section has the given parameter name
* <code>parameterName</code> and false otherwise.
*
* @param parameterName the parameter name.
*
* @return true if section has the given parameter name
* <code>parameterName</code> and false otherwise.
*/
public boolean hasParameter(String parameterName) {
return findParameter(parameterName) != null;
}

private synchronized List<Parameter> parseParameters() {
if (parameters != null) {
return parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.redhat.qute.ls.api.QuteDataModelProjectProvider;
import com.redhat.qute.ls.api.QuteUserTagProvider;
import com.redhat.qute.ls.commons.BadLocationException;
import com.redhat.qute.ls.template.QuteTextDocument;
import com.redhat.qute.parser.template.Node;
import com.redhat.qute.parser.template.NodeKind;
import com.redhat.qute.parser.template.Parameter;
Expand All @@ -54,6 +55,7 @@
import com.redhat.qute.services.nativemode.JavaTypeFilter;
import com.redhat.qute.services.nativemode.NativeModeJavaTypeFilter;
import com.redhat.qute.utils.StringUtils;
import com.redhat.qute.utils.UserTagUtils;

/**
* A Qute project.
Expand Down Expand Up @@ -91,7 +93,7 @@ public QuteProject(ProjectInfo projectInfo, QuteDataModelProjectProvider dataMod
this.openedDocuments = new HashMap<>();
this.dataModelProvider = dataModelProvider;
this.resolvedJavaTypes = new HashMap<>();
this.tagRegistry = new UserTagRegistry(uri, templateBaseDir, userTagProvider);
this.tagRegistry = new UserTagRegistry(this, templateBaseDir, userTagProvider);
this.filterInNativeMode = new NativeModeJavaTypeFilter(this);
}

Expand Down Expand Up @@ -166,6 +168,14 @@ public void onDidCloseTextDocument(TemplateInfoProvider document) {
indexer.scanAsync(true);
}

public void onDidSaveTextDocument(QuteTextDocument document) {
UserTag userTag = getUserTag(document.getTemplate());
if (userTag != null) {
// The user tag has been saved, refresh it.
userTag.clear();
}
}

private void collectInsert(String insertParamater, Node parent, Template template, List<QuteIndex> indexes) {
if (parent.getKind() == NodeKind.Section) {
Section section = (Section) parent;
Expand Down Expand Up @@ -231,6 +241,10 @@ protected synchronized CompletableFuture<ExtendedDataModelProject> loadDataModel
return null;
}
return new ExtendedDataModelProject(project);
})
.thenApply(p -> {
tagRegistry.refreshDataModel();
return p;
});
}

Expand Down Expand Up @@ -301,6 +315,30 @@ public UserTag findUserTag(String tagName) {
return null;
}

/**
* Returns the user tag from the given template and null otherwise.
*
* @param template the Qute template.
*
* @return the user tag from the given template and null otherwise.
*/
public UserTag getUserTag(Template template) {
if (!UserTagUtils.isUserTag(template)) {
return null;
}
String templateId = template.getTemplateId();
int index = templateId.indexOf('.');
if (index != -1) {
templateId = templateId.substring(0, index);
}
for (UserTag userTag : getSourceUserTags()) {
if (userTag.getTemplateId().equals(templateId)) {
return userTag;
}
}
return null;
}

/**
* Collect user tags suggestions.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import com.redhat.qute.ls.api.QuteProjectInfoProvider;
import com.redhat.qute.ls.api.QuteResolvedJavaTypeProvider;
import com.redhat.qute.ls.api.QuteUserTagProvider;
import com.redhat.qute.ls.template.QuteTextDocument;
import com.redhat.qute.parser.template.JavaTypeInfoProvider;
import com.redhat.qute.parser.template.LiteralSupport;
import com.redhat.qute.parser.template.Template;
Expand Down Expand Up @@ -220,6 +221,16 @@ public void onDidCloseTextDocument(TemplateInfoProvider document) {
}
}
}

public void onDidSaveTextDocument(QuteTextDocument document) {
String projectUri = document.getProjectUri();
if (projectUri != null) {
QuteProject project = getProject(projectUri);
if (project != null) {
project.onDidSaveTextDocument(document);
}
}
}

public CompletableFuture<ResolvedJavaTypeInfo> resolveJavaType(String javaTypeName,
JavaTypeInfoProvider javaTypeInfo, String projectUri) {
Expand Down Expand Up @@ -1308,6 +1319,10 @@ private CompletableFuture<List<ValueResolver>> getGlobalVariablesValueResolvers(
if (project == null) {
return VALUE_RESOLVERS_NULL_FUTURE;
}
return getGlobalVariables(project);
}

public static CompletableFuture<List<ValueResolver>> getGlobalVariables(QuteProject project) {
return project.getDataModelProject() //
.thenApply(dataModel -> {
if (dataModel == null) {
Expand Down Expand Up @@ -1359,4 +1374,5 @@ public JavaTypeFilter getJavaTypeFilter(String projectUri, QuteNativeSettings na
public CompletableFuture<String> getJavadoc(QuteJavadocParams params) {
return javadocProvider.getJavadoc(params);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package com.redhat.qute.project.tags;

import com.redhat.qute.commons.usertags.UserTagInfo;
import com.redhat.qute.project.QuteProject;

/**
* Binary user tag.
Expand All @@ -24,8 +25,8 @@ public class BinaryUserTag extends UserTag {
private final String uri;
private final String content;

public BinaryUserTag(UserTagInfo tagInfo) {
super(tagInfo.getFileName());
public BinaryUserTag(UserTagInfo tagInfo, QuteProject project) {
super(tagInfo.getFileName(), project);
this.uri = tagInfo.getUri();
this.content = tagInfo.getContent();
}
Expand All @@ -35,4 +36,8 @@ public String getUri() {
return uri;
}

@Override
public String getContent() {
return content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.stream.Collectors;

import com.redhat.qute.ls.commons.snippets.SnippetRegistry;
import com.redhat.qute.project.QuteProject;

/**
* User tag completion based on source 'sr/main/resources/templates/tags'
Expand All @@ -38,8 +39,9 @@ public class QuteCompletionsForSourceUserTagSection extends QuteCompletionsForUs
* Loop for files from src/main/resources/tags to update list of user tags.
*
* @param tagsDir the src/main/resources/tags directory.
* @param project
*/
public void refresh(Path tagsDir) {
public void refresh(Path tagsDir, QuteProject project) {
if (!Files.exists(tagsDir)) {
return;
}
Expand All @@ -50,7 +52,7 @@ public void refresh(Path tagsDir) {
// create all user tags
Files.list(tagsDir) //
.forEach(path -> {
snippetRegistry.registerSnippet(createUserTag(path, tagsDir));
snippetRegistry.registerSnippet(createUserTag(path, tagsDir, project));
});
} else {
// Remove all user tags which doesn't exist anymore
Expand All @@ -68,7 +70,7 @@ public void refresh(Path tagsDir) {
Files.list(tagsDir) //
.forEach(path -> {
if (!existingSnippetPaths.contains(path)) {
snippetRegistry.registerSnippet(createUserTag(path, tagsDir));
snippetRegistry.registerSnippet(createUserTag(path, tagsDir, project));
}
});
}
Expand All @@ -78,8 +80,19 @@ public void refresh(Path tagsDir) {

}

private static UserTag createUserTag(Path path, Path tagsDir) {
private static UserTag createUserTag(Path path, Path tagsDir, QuteProject project) {
String fileName = path.getName(path.getNameCount() - 1).toString();
return new SourceUserTag(fileName, path);
return new SourceUserTag(fileName, path, project);
}

/**
* Clear cache of all user tag.
*/
public void clear() {
SnippetRegistry<UserTag> snippetRegistry = super.getSnippetRegistry();
List<UserTag> snippets = snippetRegistry.getSnippets();
for (UserTag userTag : snippets) {
userTag.clear();
}
}
}
Loading

0 comments on commit 65ec61d

Please sign in to comment.