AutoSuggest provider - get current cursor position

Hello all,

I am developing an AutoSuggest provider and am wondering if it is possible to get the current cursor position in the target segment when the GetSuggestions method gets called (i.e., when the user moves the cursor in the editor).

The reason is that right now, the only way I can figure out what the text before the cursor is (which is needed for my implementation) is if the cursor is at the end of all the text in the target segment (by checking the length of the target text).  So in other words, if the target box gets pre-populated with text from a TM or other provider, and the cursor is in the middle of that text, I cannot figure out how to get AutoSuggest entries for the current cursor position (since I only know how to get them for the end of all the existing text).

Hopefully that makes sense, because I'm not sure if I'm making my use case clear enough.  In any event, to keep it very simple, I just need to know the current cursor position in this method and to find out if there is any way at all to get this info. or to pass it there from some other part of the code of the AutoSuggest provider.

Parents
  • Hi Patrick,

    AFAIK I think the correct answer for this is "There is no public API for this". Hopefully, SDL will decide to expose one in the future.

    Then there is the hackish not recommended way using Reflection (because the non-public API can change):

    EditorController editorController = SdlTradosStudio.Application.GetController<EditorController>();
    Document doc = editorController.ActiveDocument;
    var targetSelection = doc.Selection.Target;
    MethodInfo method = targetSelection.GetType().GetMethod("GetActiveRange", BindingFlags.NonPublic | BindingFlags.Instance);
    ContentRange contentRange = (ContentRange)method.Invoke(targetSelection, null);
    var offset = contentRange.Upto.TextOffset;
    
    

     There is a handy internal method called "GetActiveRange" which would be exactly what you need, but its an internal method.

    The "ContentRange" which has the info is located in the Sdl.DesktopEditor.EditorApi dll, so you will need to add the reference.

Reply
  • Hi Patrick,

    AFAIK I think the correct answer for this is "There is no public API for this". Hopefully, SDL will decide to expose one in the future.

    Then there is the hackish not recommended way using Reflection (because the non-public API can change):

    EditorController editorController = SdlTradosStudio.Application.GetController<EditorController>();
    Document doc = editorController.ActiveDocument;
    var targetSelection = doc.Selection.Target;
    MethodInfo method = targetSelection.GetType().GetMethod("GetActiveRange", BindingFlags.NonPublic | BindingFlags.Instance);
    ContentRange contentRange = (ContentRange)method.Invoke(targetSelection, null);
    var offset = contentRange.Upto.TextOffset;
    
    

     There is a handy internal method called "GetActiveRange" which would be exactly what you need, but its an internal method.

    The "ContentRange" which has the info is located in the Sdl.DesktopEditor.EditorApi dll, so you will need to add the reference.

Children