-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FUSETOOLS2-536 - provide completion for open-api in Camel K modeline
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
Showing
7 changed files
with
266 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...hub/cameltooling/lsp/internal/modelinemodel/CamelKModelineLocalResourceRelatedOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
.../java/com/github/cameltooling/lsp/internal/modelinemodel/CamelKModelineOpenAPIOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.