2

我编写了一个调用 RSyntaxTextArea 和自动完成的代码。当用户输入函数时,该单词将被替换为函数<>。我想在下面的代码中突出显示“标签”一词,以便用户知道他们必须输入一个名称。

provider.addCompletion(new ShorthandCompletion(provider, "function", "function<Label>","function<Label>"));

该代码基本上用函数替换了函数并继续前进,而不是突出显示标签并等待用户输入内容。我怎样才能做到这一点?

4

1 回答 1

1

有一个TemplateCompletion类可以用来代替ShorthandCompletion. 不幸的是,这似乎对我不起作用(在AutoCompleteDemo示例中尝试过)。

另一种方法是使用 aCodeTemplateManager并扩展CodeTemplate. 下面是一个示例(来自CodeTemplateDemo)。

首先扩展AbstractCodeTemplate

import java.io.IOException;
import java.io.ObjectInputStream;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.Element;

import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.RSyntaxUtilities;

import org.fife.ui.rsyntaxtextarea.templates.*;

public class ArtemisCodeTemplate extends StaticCodeTemplate {

    private static final long serialVersionUID = 1;

    /**
     * The code inserted before the caret position.
     */
    private String beforeCaret;

    /**
     * The code inserted after the caret position.
     */
    private String afterCaret;

    /**
     * Cached value representing whether <code>beforeCaret</code> contains
     * one or more newlines.
     */
    private transient int firstBeforeNewline;

    /**
     * Cached value representing whether <code>afterCaret</code> contains
     * one or more newlines.
     */
    private transient int firstAfterNewline;

    private static final String EMPTY_STRING        = "";

    /**
    * Placeholder for the selected keyword
    */
    private String placeholder;

    /**
     * Constructor.  This constructor only exists to support persistance
     * through serialization.
     */
    public ArtemisCodeTemplate() {
    }


    /**
     * Constructor.
     *
     * @param id The ID of this code template.
     * @param beforeCaret The text to place before the caret.
     * @param afterCaret The text to place after the caret.
     */
    public ArtemisCodeTemplate(String id, String beforeCaret, String placeholder, String afterCaret){
        super(id, beforeCaret, afterCaret);
        setPlaceholder(placeholder);
    }

    public String getPlaceholder() {
        return placeholder;
    }

    public void setPlaceholder(String text) {
        this.placeholder = text;
    }

    private String getAfterTextIndented(String indent) {
        return getTextIndented(getAfterCaretText(), firstAfterNewline, indent);
    }

    private String getBeforeTextIndented(String indent) {
        return getTextIndented(getBeforeCaretText(),firstBeforeNewline,indent);
    }

    private String getTextIndented(String text,int firstNewline,String indent) {
        if (firstNewline==-1) {
            return text;
        }
        int pos = 0;
        int old = firstNewline+1;
        StringBuffer sb = new StringBuffer(text.substring(0, old));
        sb.append(indent);
        while ((pos=text.indexOf('\n', old))>-1) {
            sb.append(text.substring(old, pos+1));
            sb.append(indent);
            old = pos+1;
        }
        if (old<text.length()) {
            sb.append(text.substring(old));
        }
        return sb.toString();
    }

    public void invoke(RSyntaxTextArea textArea) throws BadLocationException {

        Caret c = textArea.getCaret();
        int dot = c.getDot();
        int mark = c.getMark();
        int p0 = Math.min(dot, mark);
        int p1 = Math.max(dot, mark);
        RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument();
        Element map = doc.getDefaultRootElement();

        int lineNum = map.getElementIndex(dot);
        Element line = map.getElement(lineNum);
        int start = line.getStartOffset();
        int end = line.getEndOffset()-1; // Why always "-1"?
        String s = textArea.getText(start,end-start);
        int len = s.length();

        // endWS is the end of the leading whitespace
        // of the current line.
        int endWS = 0;
        while (endWS<len && RSyntaxUtilities.isWhitespace(s.charAt(endWS))) {
            endWS++;
        }
        s = s.substring(0, endWS);
        p0 -= getID().length();
        String beforeText = getBeforeTextIndented(s);
        String afterText = getAfterTextIndented(s);
        doc.replace(p0,p1-p0, beforeText+ placeholder + afterText, null);
        textArea.setCaretPosition(p0+beforeText.length());
        textArea.moveCaretPosition(p0+beforeText.length() + placeholder.length());
    }

    private void readObject(ObjectInputStream in) throws ClassNotFoundException,
                                                IOException  {
            in.defaultReadObject();
            // "Resetting" before and after text to the same values will replace
            // nulls with empty srings, and set transient "first*Newline" values.
            setBeforeCaretText(this.beforeCaret);
            setAfterCaretText(this.afterCaret);
        }

    /**
     * Returns a string representation of this template for debugging
     * purposes.
     *
     * @return A string representation of this template.
     */
    public String toString() {
        return "[ArtemisCodeTemplate: id=" + getID() +
            ", text=" + getBeforeCaretText() + "|" +
                 getPlaceholder() + "|" + getAfterCaretText() + "]";
    }


}

然后将其添加到CodeTemplateManager

  RSyntaxTextArea.setTemplatesEnabled(true);
  CodeTemplateManager ctm = RSyntaxTextArea.getCodeTemplateManager();
  CodeTemplate ct = new ArtemisCodeTemplate("sout", "System.out.println(", "<replaceme>", ");");
  ctm.addTemplate(ct);

如果您键入“sout”并按 ctrl+shift+space,它将展开System.out.println(<replaceme>);并选择该<replaceme>部分,以便您可以在上面输入。

于 2013-03-19T13:41:51.440 回答