All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fife.rsta.ac.perl.PerlCompletionProvider Maven / Gradle / Ivy

/*
 * 03/21/2010
 *
 * Copyright (C) 2010 Robert Futrell
 * robert_futrell at users.sourceforge.net
 * http://fifesoft.com/rsyntaxtextarea
 *
 * This code is licensed under the LGPL.  See the "license.txt" file included
 * with this project.
 */
package org.fife.rsta.ac.perl;

import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;

import org.fife.rsta.ac.c.CCompletionProvider;
import org.fife.ui.autocomplete.Completion;
import org.fife.ui.autocomplete.CompletionProvider;
import org.fife.ui.autocomplete.DefaultCompletionProvider;
import org.fife.ui.autocomplete.VariableCompletion;
import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Token;


/**
 * A completion provider for Perl.  It provides:
 * 
 * 
    *
  • Auto-completion for standard Perl 5.10 functions (read from an * XML file).
  • *
  • Crude auto-completion for variables. Any variables declared or used * up to the caret position are offered, regardless of whether or not * they are in scope.
  • *
* * To toggle whether parameter assistance wraps your parameters in parens, * use the {@link #setUseParensWithFunctions(boolean)} method. * * @author Robert Futrell * @version 1.0 */ public class PerlCompletionProvider extends CCompletionProvider { private boolean useParensWithFunctions; /** * {@inheritDoc} */ protected void addShorthandCompletions(DefaultCompletionProvider codeCP) { // Add nothing for now. } /** * {@inheritDoc} */ protected CompletionProvider createCodeCompletionProvider() { DefaultCompletionProvider cp = new PerlCodeCompletionProvider(this); loadCodeCompletionsFromXml(cp); addShorthandCompletions(cp); return cp; } /** * {@inheritDoc} */ protected CompletionProvider createStringCompletionProvider() { DefaultCompletionProvider cp = new DefaultCompletionProvider(); return cp; } /** * {@inheritDoc} */ protected List getCompletionsImpl(JTextComponent comp) { List completions = super.getCompletionsImpl(comp); SortedSet varCompletions = getVariableCompletions(comp); if (varCompletions!=null) { completions.addAll(varCompletions); Collections.sort(completions); } return completions; } /** * Overridden to return the null char (meaning "no end character") if the * user doesn't want to use parens around their functions. * * @return The end character for parameters list, or the null char if * none. * @see #getUseParensWithFunctions() */ public char getParameterListEnd() { return getUseParensWithFunctions() ? ')' : 0; } /** * Overridden to return the null char (meaning "no start character") if the * user doesn't want to use parens around their functions. * * @return The start character for parameters list, or the null char if * none. * @see #getUseParensWithFunctions() */ public char getParameterListStart() { return getUseParensWithFunctions() ? '(' : ' '; } /** * Returns whether the user wants to use parens around parameters to * functions. * * @return Whether to use parens around parameters to functions. * @see #setUseParensWithFunctions(boolean) */ public boolean getUseParensWithFunctions() { return useParensWithFunctions; } /** * Does a crude search for variables up to the caret position. This * method does not care whether the variables are in scope at the caret * position. * * @param comp The text area. * @return The completions for variables, or null if there * were none. */ private SortedSet getVariableCompletions(JTextComponent comp) { RSyntaxTextArea textArea = (RSyntaxTextArea)comp; int dot = textArea.getCaretPosition(); int lastLine = 0; try { lastLine = textArea.getLineOfOffset(dot); } catch (BadLocationException ble) { // Never happens ble.printStackTrace(); return null; } RSyntaxDocument doc = (RSyntaxDocument)comp.getDocument(); SortedSet varCompletions = null; String text = getDefaultCompletionProvider().getAlreadyEnteredText(comp); char firstChar = text.length()==0 ? 0 : text.charAt(0); if (firstChar!='$' && firstChar!='@' && firstChar!='%') { System.out.println("DEBUG: No use matching variables, exiting"); return null; } for (int i=0; i<=lastLine; i++) { Token t = doc.getTokenListForLine(i); while (t!=null && (t.offset+t.textCount)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy