Skip to content

Commit

Permalink
[1426] "Render" the completion proposal providers for text field comp…
Browse files Browse the repository at this point in the history
…onent

Bug: #1426
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
  • Loading branch information
pcdavid committed Oct 27, 2022
1 parent 524027b commit cc09e61
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.forms;

import java.text.MessageFormat;
import java.util.Objects;

/**
* Represents a completion request for a text field.
*
* @author pcdavid
*/
public final class CompletionRequest {
/**
* The name of the variable used to pass the current text value to the completion provider implementation.
*/
public static final String CURRENT_TEXT = "currentText"; //$NON-NLS-1$

/**
* The name of the variable used to pass the cursor position to the completion provider implementation.
*/
public static final String CURSOR_POSITION = "cursorPosition"; //$NON-NLS-1$

private final String currentText;

private final int cursorPosition;

public CompletionRequest(String currentText, int cursorPosition) {
this.currentText = Objects.requireNonNull(currentText);
this.cursorPosition = Objects.checkIndex(cursorPosition, currentText.length());
}

public String getCurrentText() {
return this.currentText;
}

public int getCursorPosition() {
return this.cursorPosition;
}

@Override
public String toString() {
String pattern = "{0} '{'currentText: {1}, cursorPosition: {2}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.currentText, this.cursorPosition);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public final class Textarea extends AbstractWidget {

private TextareaStyle style;

private Function<CompletionRequest, List<CompletionProposal>> completionProposalsProvider;

private Textarea() {
// Prevent instantiation
}
Expand All @@ -51,14 +53,18 @@ public TextareaStyle getStyle() {
return this.style;
}

public Function<CompletionRequest, List<CompletionProposal>> getCompletionProposalsProvider() {
return this.completionProposalsProvider;
}

public static Builder newTextarea(String id) {
return new Builder(id);
}

@Override
public String toString() {
String pattern = "{0} '{'id: {1}, label: {2}, value: {3}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.getId(), this.label, this.value);
String pattern = "{0} '{'id: {1}, label: {2}, value: {3}', supportsCompletion: {4}}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.getId(), this.label, this.value, this.completionProposalsProvider != null);
}

/**
Expand All @@ -80,6 +86,8 @@ public static final class Builder {

private TextareaStyle style;

private Function<CompletionRequest, List<CompletionProposal>> completionProposalsProvider;

private List<Diagnostic> diagnostics;

private Builder(String id) {
Expand Down Expand Up @@ -111,6 +119,11 @@ public Builder style(TextareaStyle style) {
return this;
}

public Builder completionProposalsProvider(Function<CompletionRequest, List<CompletionProposal>> completionProposalsProvider) {
this.completionProposalsProvider = Objects.requireNonNull(completionProposalsProvider);
return this;
}

public Builder diagnostics(List<Diagnostic> diagnostics) {
this.diagnostics = Objects.requireNonNull(diagnostics);
return this;
Expand All @@ -124,6 +137,7 @@ public Textarea build() {
textarea.value = Objects.requireNonNull(this.value);
textarea.newValueHandler = Objects.requireNonNull(this.newValueHandler);
textarea.style = this.style; // Optional on purpose
textarea.completionProposalsProvider = this.completionProposalsProvider; // Optional on purpose
textarea.diagnostics = Objects.requireNonNull(this.diagnostics);
return textarea;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public final class Textfield extends AbstractWidget {

private TextfieldStyle style;

private Function<CompletionRequest, List<CompletionProposal>> completionProposalsProvider;

private Textfield() {
// Prevent instantiation
}
Expand All @@ -51,14 +53,18 @@ public TextfieldStyle getStyle() {
return this.style;
}

public Function<CompletionRequest, List<CompletionProposal>> getCompletionProposalsProvider() {
return this.completionProposalsProvider;
}

public static Builder newTextfield(String id) {
return new Builder(id);
}

@Override
public String toString() {
String pattern = "{0} '{'id: {1}, label: {2}, value: {3}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.getId(), this.label, this.value);
String pattern = "{0} '{'id: {1}, label: {2}, value: {3}', supportsCompletion: {4}}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.getId(), this.label, this.value, this.completionProposalsProvider != null);
}

/**
Expand All @@ -80,6 +86,8 @@ public static final class Builder {

private TextfieldStyle style;

private Function<CompletionRequest, List<CompletionProposal>> completionProposalsProvider;

private List<Diagnostic> diagnostics;

private Builder(String id) {
Expand Down Expand Up @@ -111,6 +119,11 @@ public Builder style(TextfieldStyle style) {
return this;
}

public Builder completionProposalsProvider(Function<CompletionRequest, List<CompletionProposal>> completionProposalsProvider) {
this.completionProposalsProvider = Objects.requireNonNull(completionProposalsProvider);
return this;
}

public Builder diagnostics(List<Diagnostic> diagnostics) {
this.diagnostics = Objects.requireNonNull(diagnostics);
return this;
Expand All @@ -124,6 +137,7 @@ public Textfield build() {
textfield.value = Objects.requireNonNull(this.value);
textfield.newValueHandler = Objects.requireNonNull(this.newValueHandler);
textfield.style = this.style; // Optional on purpose
textfield.completionProposalsProvider = this.completionProposalsProvider; // Optional on purpose
textfield.diagnostics = Objects.requireNonNull(this.diagnostics);
return textfield;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.Objects;
import java.util.function.Function;

import org.eclipse.sirius.components.forms.CompletionProposal;
import org.eclipse.sirius.components.forms.CompletionRequest;
import org.eclipse.sirius.components.forms.description.TextareaDescription;
import org.eclipse.sirius.components.forms.elements.TextareaElementProps;
import org.eclipse.sirius.components.forms.elements.TextareaElementProps.Builder;
Expand All @@ -27,7 +29,7 @@
import org.eclipse.sirius.components.representations.VariableManager;

/**
* The component used to create the textarea widget.
* The component used to render the textarea widget.
*
* @author sbegaudeau
*/
Expand Down Expand Up @@ -62,6 +64,15 @@ public Element render() {
.newValueHandler(specializedHandler)
.children(children);

if (textareaDescription.getCompletionProposalsProvider() != null) {
Function<CompletionRequest, List<CompletionProposal>> proposalsProvider = request -> {
VariableManager completionVariables = variableManager.createChild();
completionVariables.put(CompletionRequest.CURRENT_TEXT, request.getCurrentText());
completionVariables.put(CompletionRequest.CURSOR_POSITION, request.getCursorPosition());
return textareaDescription.getCompletionProposalsProvider().apply(completionVariables);
};
textareaElementPropsBuilder.completionProposalsProvider(proposalsProvider);
}
if (iconURL != null) {
textareaElementPropsBuilder.iconURL(iconURL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

import java.util.List;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;

import org.eclipse.sirius.components.forms.CompletionProposal;
import org.eclipse.sirius.components.forms.CompletionRequest;
import org.eclipse.sirius.components.forms.description.TextfieldDescription;
import org.eclipse.sirius.components.forms.elements.TextfieldElementProps;
import org.eclipse.sirius.components.forms.elements.TextfieldElementProps.Builder;
Expand Down Expand Up @@ -49,9 +50,8 @@ public Element render() {
String label = textfieldDescription.getLabelProvider().apply(variableManager);
String iconURL = textfieldDescription.getIconURLProvider().apply(variableManager);
String value = textfieldDescription.getValueProvider().apply(variableManager);
BiFunction<VariableManager, String, IStatus> genericHandler = textfieldDescription.getNewValueHandler();
Function<String, IStatus> specializedHandler = newValue -> {
return genericHandler.apply(variableManager, newValue);
return textfieldDescription.getNewValueHandler().apply(variableManager, newValue);
};
var textfieldStyle = textfieldDescription.getStyleProvider().apply(variableManager);

Expand All @@ -64,6 +64,15 @@ public Element render() {
.newValueHandler(specializedHandler)
.children(children);

if (textfieldDescription.getCompletionProposalsProvider() != null) {
Function<CompletionRequest, List<CompletionProposal>> proposalsProvider = request -> {
VariableManager completionVariables = variableManager.createChild();
completionVariables.put(CompletionRequest.CURRENT_TEXT, request.getCurrentText());
completionVariables.put(CompletionRequest.CURSOR_POSITION, request.getCursorPosition());
return textfieldDescription.getCompletionProposalsProvider().apply(completionVariables);
};
textfieldElementPropsBuilder.completionProposalsProvider(proposalsProvider);
}
if (iconURL != null) {
textfieldElementPropsBuilder.iconURL(iconURL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.function.Function;

import org.eclipse.sirius.components.annotations.Immutable;
import org.eclipse.sirius.components.forms.CompletionProposal;
import org.eclipse.sirius.components.forms.CompletionRequest;
import org.eclipse.sirius.components.forms.TextareaStyle;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.IProps;
Expand All @@ -42,6 +44,8 @@ public final class TextareaElementProps implements IProps {

private Function<String, IStatus> newValueHandler;

private Function<CompletionRequest, List<CompletionProposal>> completionProposalsProvider;

private TextareaStyle style;

private List<Element> children;
Expand Down Expand Up @@ -74,6 +78,10 @@ public TextareaStyle getStyle() {
return this.style;
}

public Function<CompletionRequest, List<CompletionProposal>> getCompletionProposalsProvider() {
return this.completionProposalsProvider;
}

@Override
public List<Element> getChildren() {
return this.children;
Expand All @@ -85,8 +93,8 @@ public static Builder newTextareaElementProps(String id) {

@Override
public String toString() {
String pattern = "{0} '{'id: {1}, label: {2}, value: {3}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.label, this.value);
String pattern = "{0} '{'id: {1}, label: {2}, value: {3}, supportsCompletion: {4}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.label, this.value, this.completionProposalsProvider != null);
}

/**
Expand All @@ -108,6 +116,8 @@ public static final class Builder {

private TextareaStyle style;

private Function<CompletionRequest, List<CompletionProposal>> completionProposalsProvider;

private List<Element> children;

private Builder(String id) {
Expand Down Expand Up @@ -139,6 +149,11 @@ public Builder style(TextareaStyle style) {
return this;
}

public Builder completionProposalsProvider(Function<CompletionRequest, List<CompletionProposal>> completionProposalsProvider) {
this.completionProposalsProvider = Objects.requireNonNull(completionProposalsProvider);
return this;
}

public Builder children(List<Element> children) {
this.children = Objects.requireNonNull(children);
return this;
Expand All @@ -152,6 +167,7 @@ public TextareaElementProps build() {
textareaElementProps.value = Objects.requireNonNull(this.value);
textareaElementProps.newValueHandler = Objects.requireNonNull(this.newValueHandler);
textareaElementProps.style = this.style; // Optional on purpose
textareaElementProps.completionProposalsProvider = this.completionProposalsProvider; // Optional on purpose
textareaElementProps.children = Objects.requireNonNull(this.children);
return textareaElementProps;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.function.Function;

import org.eclipse.sirius.components.annotations.Immutable;
import org.eclipse.sirius.components.forms.CompletionProposal;
import org.eclipse.sirius.components.forms.CompletionRequest;
import org.eclipse.sirius.components.forms.TextfieldStyle;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.IProps;
Expand All @@ -44,6 +46,8 @@ public final class TextfieldElementProps implements IProps {

private TextfieldStyle style;

private Function<CompletionRequest, List<CompletionProposal>> completionProposalsProvider;

private List<Element> children;

private TextfieldElementProps() {
Expand All @@ -66,6 +70,10 @@ public String getValue() {
return this.value;
}

public Function<CompletionRequest, List<CompletionProposal>> getCompletionProposalsProvider() {
return this.completionProposalsProvider;
}

@Override
public List<Element> getChildren() {
return this.children;
Expand All @@ -85,8 +93,8 @@ public static Builder newTextfieldElementProps(String id) {

@Override
public String toString() {
String pattern = "{0} '{'id: {1}, label: {2}, value: {3}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.label, this.value);
String pattern = "{0} '{'id: {1}, label: {2}, value: {3}, supportsCompletion: {4}'}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.label, this.value, this.completionProposalsProvider != null);
}

/**
Expand All @@ -108,6 +116,8 @@ public static final class Builder {

private TextfieldStyle style;

private Function<CompletionRequest, List<CompletionProposal>> completionProposalsProvider;

private List<Element> children;

private Builder(String id) {
Expand Down Expand Up @@ -139,6 +149,11 @@ public Builder style(TextfieldStyle style) {
return this;
}

public Builder completionProposalsProvider(Function<CompletionRequest, List<CompletionProposal>> completionProposalsProvider) {
this.completionProposalsProvider = Objects.requireNonNull(completionProposalsProvider);
return this;
}

public Builder children(List<Element> children) {
this.children = Objects.requireNonNull(children);
return this;
Expand All @@ -152,6 +167,7 @@ public TextfieldElementProps build() {
textfieldElementProps.value = Objects.requireNonNull(this.value);
textfieldElementProps.newValueHandler = Objects.requireNonNull(this.newValueHandler);
textfieldElementProps.style = this.style; // Optional on purpose
textfieldElementProps.completionProposalsProvider = this.completionProposalsProvider; // Optional on purpose
textfieldElementProps.children = Objects.requireNonNull(this.children);
return textfieldElementProps;
}
Expand Down
Loading

0 comments on commit cc09e61

Please sign in to comment.