Finding current work on key down

3 posts, 0 answers
  1. Cameron Molyneux
    Cameron Molyneux avatar
    79 posts
    Member since:
    Jan 2010

    Posted 08 Apr 2013 Link to this post

    We are trying to implement macros so we want to track the words as we type them

    for example [T04000]

    so on key down when we see [ we start recording

    Then we listen for a ] and then whatever is in between we look up the code to get the text

    Issue is that the code to find the current word no longer seems to work

    we used
    editor.Document.CaretPosition.MoveToCurrentWordStart();
    var word = editor.Document.CaretPosition.GetCurrentWord();
    var thisLine = (Span)this.editor.Document.CaretPosition.GetCurrentInline()


    This would give us the current word AND the current line (so we can later replace the word)

    This no longer works .. our current word always comes back as new line

    We are using 2013 Q1 (not sp1 yet)

    Any ideas?

    (I will post also to support as this is urgent for me)

    Thanks




  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 09 Apr 2013 Link to this post

    Hello Cameron,

    I am not sure what may have broken the functionality, as I don't quite understand how it worked in the first place.

    First of all, changing the caret position in an event handler such as KeyDown is not usually recommended, as it would change the current place in the editor for the end user. If you adjust the position only to be able to get the current word and inline, you could create and use a different position, e.g.:
    DocumentPosition currentWordStartPosition = new DocumentPosition(editor.Document.CaretPosition);
    currentWordStartPosition.MoveToCurrentWordStart();
    var word = currentWordStartPosition.GetCurrentWord();
    var thisLine = (Span)currentWordStartPosition.GetCurrentInline();

    When it comes to the method GetCurrentWord(), it returns the word that the position is in. If the position is just at the beginning of a word - at the place of the "|" marker in the example: "aaaaa |bbbbb" - it will return the next word - "bbbbb". That is why when you type at the end of a paragraph or at the end of the document, it will return the paragraph-end symbol. Moving the position to the current word start would not change anything in this case, as the paragraph-end symbol is a separate word and that is where the position is. You can check if the position is at the paragraph-end and get the previous word in the following way:
    DocumentPosition currentWordStartPosition = new DocumentPosition(editor.Document.CaretPosition);
    DocumentPosition currentParagraphEnd = new DocumentPosition(currentWordStartPosition);
    currentParagraphEnd.MoveToLastPositionInParagraph();
    if (currentWordStartPosition == currentParagraphEnd)
    {
        currentWordStartPosition.MoveToPrevious();
    }
    var word = currentWordStartPosition.GetCurrentWord();

    When it comes to the way you get the current line, it actually returns the current Inline, which usually is the current span. The paragraph content is split in spans depending on the applied formatting. At the same time, if you have several lines in one paragraph but they have the same formatting, then they will be represented by one span.

    Furthermore, if you get the span so as to change the text in them, this is not usually recommended. A better approach would be to use methods of the editor - i.e. select the text you want to replace, delete it and insert the new content.

    I hope this answers your question. If you have other questions or need further assistance with the select/replace approach, please let us know.


    Greetings,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Cameron Molyneux
    Cameron Molyneux avatar
    79 posts
    Member since:
    Jan 2010

    Posted 09 Apr 2013 Link to this post

    This worked fine, thanks

    My comment about it previously worked was based on a team member informing that it previously worked, if you say it couldn't have that's fine the important thing is that is works fine now :)
Back to Top