How to add support for new symbol patterns?

Hi all,

My plugin is for a language (Scheme) that allows practically any symbol in identifiers. In particular dashes are very common (like: send-request-to-server). I'd like to add support for this in two places.

The first is in the extend word selection handling - the problem is that dashes are normally considered word separators, so the first time the user presses Ctrl-W it will only select part of the identifier. I have a handler installed that correctly selects the entire identifier - can I disable the default one?

The second is in completion - I'd like to add support for CamelHumps type selection of the completion, so SRTS would select send-request-to-server. How would I go about doing this?

Thanks,
Colin

4 comments
Comment actions Permalink

I was curious so I looked into this a little. Take this with a grain of salt because I could  be wrong about all of it.

It looks like you replace the default ctrl-w handler via the editorActionHandler EP

perhaps: <editorActionHandler action="EditorSelectWord" implementationClass="" />

sources:
com.intellij.openapi.editor.actions.SelectWordAtCaretAction
com.intellij.codeInsight.editorActions.SelectWordHandler

The default camelhumps works if your identifiers are all valid Java identifiers, since they aren't, to support additional characters as identifier chars i think you will have to replace CamelHumpMatcher with basically the same implementation except with your extra chars included in the regexes (in NameUtil) and wherever Character.isJavaIdentifierStart is used.

sources:
com.intellij.psi.codeStyle.NameUtil
com.intellij.codeInsight.completion.impl.CamelHumpMatcher

Anyhow apologies if any of this is incorrect. Hope it helps.

0
Comment actions Permalink

Thanks for the reply. I've looked into this a little too and found something for the first problem (expanding the selection) - it looks like the default word selection is done in com.intellij.codeInsight.editorActions.wordSelection.WordSelectioner. This has an extension point called basicWordSelectionFilter - I can implement this to say that basic word selection should not take place in certain PSI elements, in this case my identifiers. I haven't tried this but it looks promising.

Thanks for the pointer to the CamelHumpMatcher - I'd found that but what I can't see is how to replace it. It looks like what I need to do is to implement CompletionContributor, which actually has some amazing Javadoc (including a FAQ! Bravo). I haven't had time to investigate this properly yet.

Thanks for the feedback, though! And nice work on your Lua plugin too, I haven't tried it but I did take a look at the source - there's a lot of work gone in there.

0
Comment actions Permalink

This seems to work nicely, here's the code:

public class SchemeIdentifierSelectioner implements ExtendWordSelectionHandler
{
  @Override
  public boolean canSelect(PsiElement e)
  {
    PsiElement parent = e.getParent();
    return (e instanceof SchemeIdentifier) || (parent instanceof SchemeIdentifier);
  }


  @Override
  public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor)
  {
    return Collections.singletonList(e.getTextRange());
  }


  // Don't allow normal word selection for identifiers
  public static class BasicWordRestriction implements Condition<PsiElement>
  {
    @Override
    public boolean value(PsiElement element)
    {
      PsiElement parent = element.getParent();
      return !((element instanceof SchemeIdentifier) || (parent instanceof SchemeIdentifier));
    }
  }
}


<extendWordSelectionHandler implementation="schemely.editor.selection.SchemeIdentifierSelectioner"/>
<basicWordSelectionFilter implementation="schemely.editor.selection.SchemeIdentifierSelectioner$BasicWordRestriction"/>


I haven't had a chance to investigate the completion bit properly yet, will report back when I do.

0
Comment actions Permalink

Implementing a CompletionContributor also worked nicely for the new camel hump matcher.

0

Please sign in to leave a comment.