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

[FUSETOOLS2-1673] Choice EIP implementation #861

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

joshiraez
Copy link
Contributor

Implementation of the Content Based Router on the Camel language server.

Draft to set a working build although I'll be iterating on it to add/refactor stuff for better extendability.

@joshiraez joshiraez changed the title WIP: [FUSETOOLS2-1673] Choice EIP short implementation WIP: [FUSETOOLS2-1673] Choice EIP implementation Nov 29, 2022
@@ -36,5 +36,27 @@ public String getLine(String text, int line) {
}
return null;
}

public String getTextUntilPosition(TextDocumentItem document, Position position) {
String newLine = System.getProperty("line.separator");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually Client implementation of the LSP has their own policy for end of lines. Users are defining the type of end of line in either, thier IDE settings, thei rproject config, their git config. There is no need to take of care of the line separator from the system.

Comment on lines 27 to 34
private static final String JAVA_CLASS_BLUEPRINT_CAMEL_ROUTEBUILDER_IMPORT
= "import org.apache.camel.builder.RouteBuilder;";

private static final String JAVA_CLASS_BLUEPRINT_CLASS_DECLARATION = "public class TestRoute";

private static final String JAVA_CLASS_BLUEPRINT_CAMEL_ROUTEBUILDER_EXTEMD = "extends RouteBuilder";

private static final String JAVA_CLASS_BLUEPRINT_CONFIGURE_METHOD_DECLARATION = "public void configure()";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the terminology Bluerpint should not be used here.

it will collides with the "Camel XML blueprint namespace" which is related to the Apache Aries Blueprint implementation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Blueprint can be associated with this kind of stye but I didn't know it had domain meaning. I'll change it.

.map(future -> future.join())
.flatMap(x -> x.stream())
.collect(Collectors.toList())
).thenApply(Either::forLeft);
Copy link
Contributor Author

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

}

public boolean canPutEIP(Position position) {
Pattern CHOICE_EIP_PATTERN = Pattern.compile("\\)\\s*[.[c[h[o[i[c[e]?]?]?]?]?]?]?\\(?$", Pattern.MULTILINE);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ugly :D, but I didn't feel like doing a Trie based implementation was justified with just one EIP, which was my original goal

I'll be refactoring this when I add more EIPs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CHOICE_EIP_PATTERN can be put as a static attribute. (if not, better to rename to follow convention of naming witout capital letter)

@joshiraez joshiraez changed the title WIP: [FUSETOOLS2-1673] Choice EIP implementation [FUSETOOLS2-1673] Choice EIP implementation Jan 13, 2023
Copy link
Member

@apupier apupier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you precise what is working exactly and what is left for another iteration?

From my observations:

  • Content Based Router template is provided when there is:
    • a closing parenthesis before:
      Screenshot from 2023-01-16 10-27-16
    • closing parenthesis and a dot
      image
    • a closing parenthesis and a dot and a c character:
      image
  • It doesn't appear when there are more letters. like .ch. That's fine, can be done later but in this case we can simplify the regex. But there is a test which seems to tell that it shoudl be working com.github.cameltooling.lsp.internal.completion.eip.EIPChoiceCompletionTest.testProvideInsertionMidWritingChoice(), in this case I suspect that the fact that the Range is missing is causing VS Code to filter out the completion.:
    image
  • The insertion when there is .c is adding an extra dot:
    extraDotInserted
  • Content Based Router template is provided almost at the end of the completion list. I think this can be improved in another iteration by giving Range and priority
  • For another iteration: After calling the completion, setting the cursor for next edition inside the parenthesis of the first when would be great. having all places to edit would be even better

}

public boolean canPutEIP(Position position) {
Pattern CHOICE_EIP_PATTERN = Pattern.compile("\\)\\s*[.[c[h[o[i[c[e]?]?]?]?]?]?]?\\(?$", Pattern.MULTILINE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CHOICE_EIP_PATTERN can be put as a static attribute. (if not, better to rename to follow convention of naming witout capital letter)

String newLine = System.getProperty("line.separator");
CompletionItem completion = new CompletionItem("Content Based Router");
completion.setDocumentation(
"Read more: https://camel.apache.org/components/3.11.x/eips/content-based-router-eip.html"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too old link, already a 404.

Better to link to the latest LTS, maybe https://camel.apache.org/components/3.20.x/eips/choice-eip.html

}

private CompletionItem choiceEIPcompletion() {
String newLine = System.getProperty("line.separator");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we can use \n and delegate to editor which end of line is configured.
it is even better in case of remote Camel Languaeg Server (we are not providing some right ow but ihe architecture allows it I provided proof of concept). It would mean that the client can be on a different OS than the server

}

public boolean canPutEIP(Position position) {
Pattern CHOICE_EIP_PATTERN = Pattern.compile("\\)\\s*[.[c[h[o[i[c[e]?]?]?]?]?]?]?\\(?$", Pattern.MULTILINE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sonar is complaining of duplicates in the regexp. I guess there should be a simpler way to write it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaving it out of the iteration for now as RegExr is not complaining nor I can find what would be the alternative right now. Mmm

Comment on lines 45 to 49
String textUntilPosition = "";

for(int i = 0; i<position.getLine(); i++) {
textUntilPosition += lines[i] + newLine;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sonar is recommending to use a StringBuilder

completionItem -> completionItem.getLabel().startsWith("Content Based Router")
).collect(Collectors.toList());

assertThat(completionItems).hasSize(0); //Removing camel-k modeline insertion. Refactor later
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssertJ is providing an isEmpty() method. Itwill provide a better error message, simplifying the debugging process.

).collect(Collectors.toList());


assertThat(completionItems).hasSize(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssertJ is providing an isEmpty() method. Itwill provide a better error message, simplifying the debugging process.

completionItem -> completionItem.getLabel().startsWith("Content Based Router")
).collect(Collectors.toList());

assertThat(completionItems).hasSize(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssertJ is providing an isEmpty() method. It will provide a better error message, simplifying the debugging process.

Comment on lines 34 to 37
List<CompletionItem> completionItems = getCompletionsFor(contents, position);
completionItems = completionItems.stream().filter(
completionItem -> completionItem.getLabel().startsWith("Content Based Router")
).collect(Collectors.toList());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code can be factorized.
it will be really useful in case the label of the completion is modified for instance

RouteTextBuilder.DocumentContentWithPosition document =
RouteTextBuilder.createJavaDocumentClass("");

List<CompletionItem> completionItems = getCompletionsFor(document.content, document.position);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Java, we usually avoid to expose attributes and use getters and setters

@joshiraez
Copy link
Contributor Author

Just saw this. I'm on with the improvements.

@joshiraez
Copy link
Contributor Author

Ok checking the range and priority things on top of all the comments.

It should work for anything between ..){{{.choice(}}}... as the test stated. I have an idea why it might be failing on .ch+

I'll get first with the comments then on the top comment priorities. Expect an update today

joshiraez and others added 11 commits January 20, 2023 16:11
… it works fine...? Leaving it as is because I would have to use capturing group which have a higher load to fix it
…ere written to just have "\n" independently on the system so they broke. It didn't make sense to leave the carriage return as it would be treated as another whitespace character so I decided to simplify it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants