-
Notifications
You must be signed in to change notification settings - Fork 25
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
[FUSETOOLS2-1673] Choice EIP implementation #861
base: main
Are you sure you want to change the base?
Changes from all commits
00720d6
d8f0ec9
6d89bcc
943bf57
98a3e97
c077274
2072ef7
db24826
e512c5e
1c72b3d
45127a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.github.cameltooling.lsp.internal.parser; | ||
|
||
import org.eclipse.lsp4j.CompletionItem; | ||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4j.TextDocumentItem; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.regex.Pattern; | ||
|
||
public class CamelEIPParser { | ||
|
||
private static final String LATEST_LTS_URL = "https://camel.apache.org/components/3.20.x"; | ||
private final TextDocumentItem document; | ||
|
||
public CamelEIPParser(TextDocumentItem textDocumentItem) { | ||
this.document = textDocumentItem; | ||
} | ||
|
||
public boolean canPutEIP(Position position) { | ||
Pattern choiceEipPattern = Pattern.compile("\\)\\s*[.[c[h[o[i[c[e]?]?]?]?]?]?]?\\(?$", Pattern.MULTILINE); | ||
ParserFileHelperUtil util = new ParserFileHelperUtil(); | ||
String textUntilPosition = util.getTextUntilPosition(document, position); | ||
|
||
return choiceEipPattern.matcher(textUntilPosition).find(); | ||
} | ||
|
||
public CompletableFuture<List<CompletionItem>> getCompletions() { | ||
return CompletableFuture.completedFuture( | ||
List.of(choiceEIPcompletion()) | ||
); | ||
} | ||
|
||
private CompletionItem choiceEIPcompletion() { | ||
String newLine = "\n"; | ||
CompletionItem completion = new CompletionItem("Content Based Router"); | ||
completion.setDocumentation( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be done in another iteration. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving it for other iteration. I know what you refer, i'll pushing a first iteration with most of the comment changes and iterate it further later today There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think providing a bit more details on what is the Content based router with a single sentence would help users. This can be done in another iteration. |
||
"Read more: "+LATEST_LTS_URL+"/eips/choice-eip.html" | ||
); | ||
completion.setInsertText( | ||
".choice()" + newLine + | ||
".when()" + newLine + | ||
".to()" + newLine + | ||
".when()" + newLine + | ||
".to()" + newLine + | ||
".otherwise()" + newLine + | ||
".to()" | ||
); | ||
|
||
return completion; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.github.cameltooling.lsp.internal.completion.eip; | ||
|
||
import com.github.cameltooling.lsp.internal.AbstractCamelLanguageServerTest; | ||
import com.github.cameltooling.lsp.internal.CamelLanguageServer; | ||
import com.github.cameltooling.lsp.internal.util.RouteTextBuilder; | ||
import org.eclipse.lsp4j.CompletionItem; | ||
import org.eclipse.lsp4j.CompletionList; | ||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4j.jsonrpc.messages.Either; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
|
||
class EIPChoiceCompletionTest extends AbstractCamelLanguageServerTest { | ||
|
||
private static final String FROM_ROUTE = "from(\"timer:foo?period={{timer.period}}\")"; | ||
private static final String FROM_ROUTE_WITH_LINE_BREAKS_AND_TABS | ||
= "from(\"timer:foo?period={{timer.period}}\")\n\t.bean()\n"; | ||
|
||
private static final String FROM_ROUTE_WITH_CHOICE_EIP_MID_WRITTEN | ||
= "from(\"timer:foo?period={{timer.period}}\")\n\t.ch\n"; | ||
public static final String CONTENT_BASED_ROUTER_LABEL = "Content Based Router"; | ||
|
||
|
||
@Test | ||
void testDontProvideInsertionOnEmptyJavaFile() throws Exception { | ||
String contents = ""; | ||
Position position = new Position(0,0); | ||
|
||
List<CompletionItem> completionItems = getCompletionsFor(contents, position); | ||
completionItems = getContentBasedRouterCompletionItems(completionItems); | ||
|
||
assertThat(completionItems).isEmpty(); | ||
} | ||
|
||
@Test | ||
void testProvideInsertionOnEmptyJavaClass() throws Exception { | ||
RouteTextBuilder.DocumentContentWithPosition document = | ||
RouteTextBuilder.createJavaDocumentClass(""); | ||
|
||
List<CompletionItem> completionItems = getCompletionsFor(document.getContent(), document.getPosition()); | ||
completionItems = getContentBasedRouterCompletionItems(completionItems); | ||
|
||
|
||
assertThat(completionItems).isEmpty(); | ||
} | ||
|
||
@Test | ||
void testProvideInsertionOnEmptyJavaCamelRoute() throws Exception { | ||
RouteTextBuilder.DocumentContentWithPosition document = | ||
RouteTextBuilder.createJavaDocumentCamelRoute(""); | ||
|
||
List<CompletionItem> completionItems = getCompletionsFor(document.getContent(), document.getPosition()); | ||
completionItems = getContentBasedRouterCompletionItems(completionItems); | ||
|
||
assertThat(completionItems).hasSize(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AssertJ is providing an |
||
} | ||
|
||
@Test | ||
void testProvideInsertionAfterFromOnCamelRoute() throws Exception { | ||
RouteTextBuilder.DocumentContentWithPosition document = | ||
RouteTextBuilder.createJavaDocumentCamelRoute(FROM_ROUTE); | ||
|
||
List<CompletionItem> completionItems = getCompletionsFor(document.getContent(), document.getPosition()); | ||
completionItems = getContentBasedRouterCompletionItems(completionItems); | ||
|
||
|
||
assertThat(completionItems).hasSize(1); | ||
} | ||
|
||
@Test | ||
void testProvideInsertionAfterFromOnCamelRouteWithLineBreaks() throws Exception { | ||
RouteTextBuilder.DocumentContentWithPosition document = | ||
RouteTextBuilder.createJavaDocumentCamelRoute(FROM_ROUTE_WITH_LINE_BREAKS_AND_TABS); | ||
|
||
List<CompletionItem> completionItems = getCompletionsFor(document.getContent(), document.getPosition()); | ||
completionItems = getContentBasedRouterCompletionItems(completionItems); | ||
|
||
|
||
assertThat(completionItems).hasSize(1); | ||
} | ||
|
||
@Test | ||
void testProvideInsertionMidWritingChoice() throws Exception { | ||
RouteTextBuilder.DocumentContentWithPosition document = | ||
RouteTextBuilder.createJavaDocumentCamelRoute(FROM_ROUTE_WITH_CHOICE_EIP_MID_WRITTEN); | ||
|
||
List<CompletionItem> completionItems = getCompletionsFor(document.getContent(), document.getPosition()); | ||
completionItems = getContentBasedRouterCompletionItems(completionItems); | ||
|
||
|
||
assertThat(completionItems).hasSize(1); | ||
} | ||
|
||
|
||
private List<CompletionItem> getCompletionsFor(String contents, Position position) throws Exception { | ||
CamelLanguageServer camelLanguageServer = initializeLanguageServer(contents, ".java"); | ||
|
||
CompletableFuture<Either<List<CompletionItem>, CompletionList>> completions = getCompletionFor( | ||
camelLanguageServer, position); | ||
|
||
return completions.get().getLeft(); | ||
} | ||
|
||
@NotNull | ||
private List<CompletionItem> getContentBasedRouterCompletionItems(List<CompletionItem> completionItems) { | ||
|
||
return completionItems.stream().filter( | ||
completionItem -> completionItem.getLabel().startsWith(CONTENT_BASED_ROUTER_LABEL) | ||
).collect(Collectors.toList()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed because we want to be able to add different completions that have different triggers