Skip to content

Commit

Permalink
FUSETOOLS2-536 - provide completion for open-api in Camel K modeline
Browse files Browse the repository at this point in the history
filters on json, yaml and yml files as a first iteration. We don' tknow
yet what is the precise filter for Open APi spec v2.

Signed-off-by: Aurélien Pupier <apupier@redhat.com>
  • Loading branch information
apupier committed Sep 14, 2020
1 parent 702d1fc commit a73f9a6
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ private CamelKModelineOptionNames() {}
public static final String OPTION_NAME_PROPERTY = "property";
public static final String OPTION_NAME_PROPERTY_FILE = "property-file";
public static final String OPTION_NAME_RESOURCE = "resource";
public static final String OPTION_NAME_OPEN_API = "open-api";

static {
OPTION_NAMES_WITH_DESCRIPTION = new HashMap<>();
OPTION_NAMES_WITH_DESCRIPTION.put(OPTION_NAME_DEPENDENCY, "An external library that should be included. E.g. for Maven dependencies \"dependency=mvn:org.my/app:1.0\"");
OPTION_NAMES_WITH_DESCRIPTION.put("env", "Set an environment variable in the integration container. E.g \"env=MY_VAR=my-value\"");
OPTION_NAMES_WITH_DESCRIPTION.put("label", "Add a label to the integration. E.g. \"label=my.company=hello\"");
OPTION_NAMES_WITH_DESCRIPTION.put("name", "The integration name");
OPTION_NAMES_WITH_DESCRIPTION.put("open-api", "Add an OpenAPI v2 spec (file path)");
OPTION_NAMES_WITH_DESCRIPTION.put(OPTION_NAME_OPEN_API, "Add an OpenAPI v2 spec (file path)");
OPTION_NAMES_WITH_DESCRIPTION.put("profile", "Trait profile used for deployment");
OPTION_NAMES_WITH_DESCRIPTION.put(OPTION_NAME_PROPERTY, "Add a camel property");
OPTION_NAMES_WITH_DESCRIPTION.put(OPTION_NAME_PROPERTY_FILE, "Bind a property file to the integration. E.g. \"property-file=integration.properties\"");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.cameltooling.lsp.internal.modelinemodel;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.camel.catalog.CamelCatalog;
import org.eclipse.lsp4j.CompletionItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.cameltooling.lsp.internal.completion.CompletionResolverUtils;

public abstract class CamelKModelineLocalResourceRelatedOption implements ICamelKModelineOptionValue {

private static final Logger LOGGER = LoggerFactory.getLogger(CamelKModelineLocalResourceRelatedOption.class);

private String value;
private int startPosition;
private String documentItemUri;

public CamelKModelineLocalResourceRelatedOption(String value, int startPosition, String documentItemUri) {
this.value = value;
this.startPosition = startPosition;
this.documentItemUri = documentItemUri;
}

@Override
public int getStartPositionInLine() {
return startPosition;
}

@Override
public int getEndPositionInLine() {
return getStartPositionInLine() + value.length();
}

@Override
public String getValueAsString() {
return value;
}

protected abstract String getPropertyName();
protected abstract Predicate<? super Path> getFilter();

@Override
public CompletableFuture<List<CompletionItem>> getCompletions(int position, CompletableFuture<CamelCatalog> camelCatalog) {
try {
Path documentUriPath = Paths.get(new URI(documentItemUri));
if(documentUriPath != null && documentUriPath.toFile().exists()) {
Path documentUriParentPath = documentUriPath.getParent();
if(documentUriParentPath != null && documentUriParentPath.toFile().exists()) {
return CompletableFuture.completedFuture(retrieveCompletionItemsForPotentialFiles(documentUriPath, documentUriParentPath));
}
}
} catch (URISyntaxException | IllegalArgumentException | IOException exception) {
LOGGER.debug("Cannot provide completion for " + getPropertyName() + " parameter", exception);
}
return ICamelKModelineOptionValue.super.getCompletions(position, camelCatalog);
}

private List<CompletionItem> retrieveCompletionItemsForPotentialFiles(Path documentUriPath, Path documentUriParentPath) throws IOException {
try (Stream<Path> pathStream = Files.walk(documentUriParentPath)){
return pathStream
.filter(Files::isRegularFile)
.filter(path -> !path.equals(documentUriPath))
.filter(getFilter())
.map(documentUriParentPath::relativize)
.map(Path::toString)
.map(CompletionItem::new)
.map(completionItem -> {
CompletionResolverUtils.applyTextEditToCompletionItem(this, completionItem);
return completionItem;
})
.collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.cameltooling.lsp.internal.modelinemodel;

import java.nio.file.Path;
import java.util.function.Predicate;

import com.github.cameltooling.lsp.internal.completion.modeline.CamelKModelineOptionNames;

public class CamelKModelineOpenAPIOption extends CamelKModelineLocalResourceRelatedOption {

public CamelKModelineOpenAPIOption(String value, int startPosition, String documentItemUri) {
super(value, startPosition, documentItemUri);
}

@Override
protected String getPropertyName() {
return CamelKModelineOptionNames.OPTION_NAME_OPEN_API;
}

@Override
protected Predicate<? super Path> getFilter() {
return path -> {
String filename = path.getFileName().toString();
return filename.endsWith(".json")
|| filename.endsWith(".yaml")
|| filename.endsWith(".yml");
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ private ICamelKModelineOptionValue createOptionValue(String option, int nameValu
return new CamelKModelinePropertyFileOption(value, startPosition, documentItem.getUri());
} else if(CamelKModelineOptionNames.OPTION_NAME_RESOURCE.equals(optionName)) {
return new CamelKModelineResourceOption(value, startPosition, documentItem.getUri());
} else {
} else if(CamelKModelineOptionNames.OPTION_NAME_OPEN_API.equals(optionName)) {
return new CamelKModelineOpenAPIOption(value, startPosition, documentItem.getUri());
}else {
return new GenericCamelKModelineOptionValue(value, startPosition);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,82 +16,23 @@
*/
package com.github.cameltooling.lsp.internal.modelinemodel;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.function.Predicate;

import org.apache.camel.catalog.CamelCatalog;
import org.eclipse.lsp4j.CompletionItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.cameltooling.lsp.internal.completion.modeline.CamelKModelineOptionNames;

import com.github.cameltooling.lsp.internal.completion.CompletionResolverUtils;

public class CamelKModelinePropertyFileOption implements ICamelKModelineOptionValue {
public class CamelKModelinePropertyFileOption extends CamelKModelineLocalResourceRelatedOption {

private static final Logger LOGGER = LoggerFactory.getLogger(CamelKModelinePropertyFileOption.class);
private String value;
private int startPosition;
private String documentItemUri;

public CamelKModelinePropertyFileOption(String value, int startPosition, String documentItemUri) {
this.value = value;
this.startPosition = startPosition;
this.documentItemUri = documentItemUri;
}

@Override
public int getStartPositionInLine() {
return startPosition;
}

@Override
public int getEndPositionInLine() {
return getStartPositionInLine() + value.length();
}

@Override
public String getValueAsString() {
return value;
super(value, startPosition, documentItemUri);
}

@Override
public CompletableFuture<List<CompletionItem>> getCompletions(int position, CompletableFuture<CamelCatalog> camelCatalog) {
try {
Path documentUriPath = Paths.get(new URI(documentItemUri));
if(documentUriPath != null && documentUriPath.toFile().exists()) {
Path documentUriParentPath = documentUriPath.getParent();
if(documentUriParentPath != null && documentUriParentPath.toFile().exists()) {
return CompletableFuture.completedFuture(retrieveCompletionItemsForPotentialFiles(documentUriPath, documentUriParentPath));
}
}
} catch (URISyntaxException | IllegalArgumentException | IOException exception) {
LOGGER.debug("Cannot provide completion for property-file parameter", exception);
}
return ICamelKModelineOptionValue.super.getCompletions(position, camelCatalog);
protected Predicate<? super Path> getFilter() {
return path -> path.getFileName().toString().endsWith(".properties");
}

private List<CompletionItem> retrieveCompletionItemsForPotentialFiles(Path documentUriPath, Path documentUriParentPath) throws IOException {
try (Stream<Path> pathStream = Files.walk(documentUriParentPath)){
return pathStream
.filter(Files::isRegularFile)
.filter(path -> !path.equals(documentUriPath))
.filter(path -> path.getFileName().toString().endsWith(".properties"))
.map(documentUriParentPath::relativize)
.map(Path::toString)
.map(CompletionItem::new)
.map(completionItem -> {
CompletionResolverUtils.applyTextEditToCompletionItem(this, completionItem);
return completionItem;
})
.collect(Collectors.toList());
}
@Override
protected String getPropertyName() {
return CamelKModelineOptionNames.OPTION_NAME_PROPERTY_FILE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,81 +16,24 @@
*/
package com.github.cameltooling.lsp.internal.modelinemodel;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.function.Predicate;

import org.apache.camel.catalog.CamelCatalog;
import org.eclipse.lsp4j.CompletionItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.cameltooling.lsp.internal.completion.modeline.CamelKModelineOptionNames;

import com.github.cameltooling.lsp.internal.completion.CompletionResolverUtils;

public class CamelKModelineResourceOption implements ICamelKModelineOptionValue {
public class CamelKModelineResourceOption extends CamelKModelineLocalResourceRelatedOption {

private static final Logger LOGGER = LoggerFactory.getLogger(CamelKModelineResourceOption.class);
private String value;
private int startPosition;
private String documentItemUri;

public CamelKModelineResourceOption(String value, int startPosition, String documentItemUri) {
this.value = value;
this.startPosition = startPosition;
this.documentItemUri = documentItemUri;
}

@Override
public int getStartPositionInLine() {
return startPosition;
super(value, startPosition, documentItemUri);
}

@Override
public int getEndPositionInLine() {
return getStartPositionInLine() + value.length();
protected String getPropertyName() {
return CamelKModelineOptionNames.OPTION_NAME_RESOURCE;
}

@Override
public String getValueAsString() {
return value;
}

@Override
public CompletableFuture<List<CompletionItem>> getCompletions(int position, CompletableFuture<CamelCatalog> camelCatalog) {
try {
Path documentUriPath = Paths.get(new URI(documentItemUri));
if(documentUriPath != null && documentUriPath.toFile().exists()) {
Path documentUriParentPath = documentUriPath.getParent();
if(documentUriParentPath != null && documentUriParentPath.toFile().exists()) {
return CompletableFuture.completedFuture(retrieveCompletionItemsForPotentialFiles(documentUriPath, documentUriParentPath));
}
}
} catch (URISyntaxException | IllegalArgumentException | IOException exception) {
LOGGER.debug("Cannot provide completion for resource parameter", exception);
}
return ICamelKModelineOptionValue.super.getCompletions(position, camelCatalog);
}

private List<CompletionItem> retrieveCompletionItemsForPotentialFiles(Path documentUriPath, Path documentUriParentPath) throws IOException {
try (Stream<Path> pathStream = Files.walk(documentUriParentPath)){
return pathStream
.filter(Files::isRegularFile)
.filter(path -> !path.equals(documentUriPath))
.map(documentUriParentPath::relativize)
.map(Path::toString)
.map(CompletionItem::new)
.map(completionItem -> {
CompletionResolverUtils.applyTextEditToCompletionItem(this, completionItem);
return completionItem;
})
.collect(Collectors.toList());
}
protected Predicate<? super Path> getFilter() {
return path -> true;
}
}
Loading

0 comments on commit a73f9a6

Please sign in to comment.