Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code completion for when clause #223

Merged
merged 8 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class Constants {
public static final String KIND_TRIGGERBINDINGS = "triggerbindings";
public static final String KIND_CLUSTERTRIGGERBINDINGS = "clustertriggerbindings";
public static final String KIND_EVENTLISTENER = "eventlisteners";
public static final String KIND_PIPELINERUNS = "pipelineruns";
public static final String KIND_TASKRUNS = "taskruns";

public static final String KIND_PIPELINE = "pipeline";
public static final String KIND_PIPELINERUN = "pipelinerun";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*******************************************************************************
* Copyright (c) 2020 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc.
******************************************************************************/
package com.redhat.devtools.intellij.tektoncd.completion;

import com.fasterxml.jackson.databind.JsonNode;
import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionProvider;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.redhat.devtools.intellij.common.utils.YAMLHelper;
import com.redhat.devtools.intellij.tektoncd.tkn.Tkn;
import com.redhat.devtools.intellij.tektoncd.utils.TreeHelper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.StreamSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BaseCompletionProvider extends CompletionProvider<CompletionParameters> {
Logger logger = LoggerFactory.getLogger(BaseCompletionProvider.class);

protected Tkn getClient(CompletionParameters parameters) {
return TreeHelper.getTkn(parameters.getEditor().getProject());
}

/**
* Return a list of tasks present in the pipeline except the current task and others if specified
*
* @param parameters data related to the current document
* @param currentTaskElement the psielement representing the task where the user is typing in
* @param tasksToExclude additional tasks to exclude from the resulting list
* @return
*/
protected List<String> getFilteredTasksInPipeline(CompletionParameters parameters, PsiElement currentTaskElement, List<String> tasksToExclude) {
List<String> tasks = new ArrayList<>();

try {
String yamlUntilTask = parameters.getEditor().getDocument().getText(new TextRange(0, currentTaskElement.getTextOffset()));
long taskPosition = 0;
try {
JsonNode tasksNodeUntilSelected = YAMLHelper.getValueFromYAML(yamlUntilTask, new String[]{"spec"} );
if (tasksNodeUntilSelected.has("tasks")) {
taskPosition = StreamSupport.stream(tasksNodeUntilSelected.get("tasks").spliterator(), true).count();
}
} catch (IOException e) {
logger.warn("Error: " + e.getLocalizedMessage());
}

// get all tasks node found in the pipeline and add valid options to lookup list
String yaml = parameters.getEditor().getDocument().getText();
JsonNode tasksNode = YAMLHelper.getValueFromYAML(yaml, new String[]{"spec", "tasks"} );
int cont = 0;
if (tasksNode != null) {
for (JsonNode item : tasksNode) {
if (item != null && cont != taskPosition) {
String name = item.has("name") ? item.get("name").asText("") : "";
if (!name.isEmpty() && !tasksToExclude.contains(name)) {
tasks.add(name);
}
}
cont++;
}
}
} catch (IOException e) {
logger.warn("Error: " + e.getLocalizedMessage());
}

return tasks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import com.intellij.codeInsight.completion.CompletionContributor;
import com.intellij.codeInsight.completion.CompletionType;
import com.intellij.patterns.PlatformPatterns;
import org.jetbrains.yaml.YAMLTokenTypes;
import org.jetbrains.yaml.psi.YAMLScalar;
import org.jetbrains.yaml.psi.YAMLPsiElement;

public class DictionaryContributor extends CompletionContributor {
public DictionaryContributor() {
Expand All @@ -30,17 +29,21 @@ public DictionaryContributor() {
extend(CompletionType.BASIC,
YamlElementPatternHelper.getMultipleLineScalarKey("runAfter"),
new RunAfterCompletionProvider());
// operator - when clause
extend(CompletionType.BASIC,
YamlElementPatternHelper.getAfterParentScalarKeyInSequence("operator", "when"),
new OperatorInWhenClauseCodeCompletion());
// resource in pipeline
extend(CompletionType.BASIC,
YamlElementPatternHelper.getAfterParentScalarKeyInSequence("resource", "resources", "inputs", "outputs"),
new ResourceInPipelineCompletionProvider());
// code completion not related to a specific place in a file. It doesn't depend on any tag
extend(CompletionType.BASIC,
PlatformPatterns
.psiElement(YAMLTokenTypes.TEXT)
.psiElement()
.withParent(
PlatformPatterns
.psiElement(YAMLScalar.class)),
.psiElement(YAMLPsiElement.class)),
new GeneralCompletionProvider());
}
}
Loading