Token - CodeCompletion

Hey,
i just wanted to know if there is an easy way to get the actual typed token on which the code completion is triggered.  

5 comments
Comment actions Permalink

Found the class TextLookupRanges with the property ShortPrefixRange. But this method is marked as obsolete. Is there any new function to retrieve the same functionality? Greetings T0a

0
Comment actions Permalink

The ShortNameRange flag has been moved out of this class, because it's really a property of the type of code completion being attempted, rather than the ranges class. This has been removed in ReSharper 8, and instead, you'd call GetPrefixRange, passing in the CodeCompletionContext. If it's not import completion, it returns the end of the insert range, otherwise, it returns the whole of the insert range.

However, if you're looking to get the token at the current context, you can get the node with:

file.FindNodeAt(selectedTreeRange)

where file is the IFile interface that represents the PSI abstract syntax tree, from the CodeCompletionContext.File property, and selectedTreeRange from CodeCompletionContext.SelectedTreeRange. You get CodeCompletionContext passed into ICodeCompletionContextProvider.GetCompletionContext. This interface is created during the code completion process. If you're not creating your own context, but using something like CSharpCodeCompletionContext, then this class already has the NodeInFile property.

The ITreeNode you get from this call will give you the ITreeNode in the syntax tree of the file. This might be a low-level token, or something a bit higher, depending on the context of the invocation, and how correct the code is at that time. You can always walk up the Parent property of the tree node to find the nearest owning piece of code.

0
Comment actions Permalink

See below my first approach. The Problem is that the AddLookupItems method deleviers me a context, where for example not all characters are recognized (Str will save S in token). Where do I get the current context? I have solved the problem this way, because I don't know who to get the ICodeCompletionContextProvider Interface which is created during the code completion process. Greetings Toa

[Language(typeof(CSharpLanguage))]
public class CodeCompletionAnalyserProcess : CSharpItemsProviderBase<CSharpCodeCompletionContext>
{
   private string token;
   private CSharpCodeCompletionContext _context;
      
   protected override bool AddLookupItems(CSharpCodeCompletionContext context, GroupedItemsCollector collector)
   {
     this._context = context;
     return base.AddLookupItems(context, collector);
   }

   private void handleCompletionCompleteEvent(object sender, ILookupItem lookupItem, Suffix suffix, LookupItemInsertType lookupItemInsertType)
   {
      ITreeNode node = _context.NodeInFile;
      token = node.GetText(new StringBuilder()).ToString();

      //...
   }
}
0
Comment actions Permalink

OK. I hadn't quite joined the dots and realised that this request was linked to this: http://devnet.jetbrains.com/message/5491924#5491924

So, that post shows how to get an event for when the code completion completes, and gives you the ILookupItem that's been chosen. Since you know what's been chosen, what do you need the context for, now?

0
Comment actions Permalink

I want to collect some developer informations e.g what was completet and therefore also how complete was the acutual token on that the completion was triggered. For example you type str and trigger the completion event for string, I want to log str as token and String as choosen completion. Just collect some informations about the completion. Greetings T0a

0

Please sign in to leave a comment.