locked
Mark Text from Code RRS feed

  • Question

  • I would like to generate Code from my Add-In and mark it afterwards,(like when you are dragging with your Mouse or pressing Shift+Arrow Keys) so if the user types in anything, the generated code will be overriden with his input.

    How to do this?(If its possible i would like to do it in an addin, but maybe i have to create a package instead?)

    Sunday, March 28, 2010 2:22 PM

Answers

  • Ok i finally found it out on my own. Turns out, that my first try was correct, but there seems to be a bug in the SDK.

    //This line selects the 4 characters to the right of the current caret position
    //The Text is selected correctly, but the active Window loses the focus-->if the user types in something, nothing happens
                    selection.CharRight(true,4);
    //This line activates the active document(a bit strange, but it works).
                    application.ActiveDocument.Activate();

     

    Maybe this only occurs in the RC1?

    • Marked as answer by kamtschatka Tuesday, April 13, 2010 8:26 PM
    Tuesday, April 13, 2010 8:26 PM

All replies

  • Hello, kamtschatka

    You don’t need a package, an add-in is enough, if we want to manipulate the text selection and replace the selected text, we usually use TextSelection and EditPoint interface.

    See the following code snippet, this is the little sample for using this.

                        Document doc = _applicationObject.ActiveDocument;

                        TextSelection txtSelect = doc.Selection as TextSelection;

                        //Select the current line where the cursor is

                        txtSelect.SelectLine();

                        //move the selectied text to the fourth column.

                        txtSelect.WordRight(false, 4);

                        //expand the selected to four line below it.

                        txtSelect.LineDown(true, 4);

                        //EditPoint is used to edit the current selection document.

                        EditPoint editPoint = txtSelect.ActivePoint.CreateEditPoint();

                        //the selected text will be replaced "replaced text"

                        editPoint.Insert("replaced text");

    Usually, we need to find the code element to cooperate with the two interfaces. Please reference this link,

    Discovering Code by Using the Code Model (Visual C#)

    Thanks

    Chao

    Tuesday, March 30, 2010 7:44 AM
  • Thank you for your answer, but unfortunately it is not working as expected.

    The Text will only be selected, if an EditPoint is created and text is inserted(and then there is still a chance, that it wont select the code). If i remove the last 2 lines from your example, nothing will be selected.

    I tried a little trick(call editPoint.Insert("")), but it is not working.

     

    What i am doing at the moment is this:

    TextSelection txtSelect=_applicationObject.ActiveDocument.Selection as TextSelection;

    txtSelect.CharRight(true, 4);

    This should select the next 4 chars after the current cursor position, but actually it does nothing.

     

    Is this a Bug in the Visual Studio Extensibility Framework? Is there another way of doing this? 

    Thanks in advance!

    Wednesday, March 31, 2010 12:46 AM
  • Hello, kamtschatka

    I believe there is no bug.

    I think you need to research the TextSelection interface and EditPoint interface.

    txtSelect.SelectLine();

    This will select the current line where the cursor is at.

    txtSelect.LineDown(true, 4);

    This will expand your selection area to four line like you press Shift+Arrow Keys.

    txtSelect.Delete(2);

    This method is used to delete the selection area. You could insert your words after you deleted. For example the following code will perform like the generated code will be overriden with his input.

                        txtSelect.Delete(2);

                        EditPoint editPoint = txtSelect.ActivePoint.CreateEditPoint();

                          editPoint.Insert("replaced text");

    If you want the following code, you must make sure your cursor position is valid.

     

    You mentioned you use the following code to select the right four letters.

    TextSelection txtSelect=_applicationObject.ActiveDocument.Selection as TextSelection;

    txtSelect.CharRight(true, 4);

    Because CharRight is used to Move the object the specified number of characters to the right, you need to change to the following

                     txtSelect.WordRight(true, 4);

    This would do the trick.

    So please research the two interfaces carefully.

    Thanks

    Chao

    Wednesday, March 31, 2010 3:05 AM
  • Hi

    Sorry i didn't read the documentation carefully enough, but still there is this issue where i have to create an editpoint and insert text, to make the selection visible.

    I will explain excactly what i want to do, because after careful reading of the documentation, i don't see a way to implement what i want to do:

    I have custom attributes, and i want to insert values:

    This is how it looks before my action: [MyAttribute("name")]

    This is how it looks after it: [MyAttribute("name", Value1=true)] (and true should be selected)

    So i don't want to select the whole line, or the whole document, just the word "true".

    I tried selecting the whole line and calling CharLeft(true, 1), until i am at the end of true, but this way the selection is lost. And then I needed a way to set the StartPoint of the Selection, but i didn't find a solution for that?

     

    Thanks

    Wednesday, March 31, 2010 2:36 PM
  • Hello, again

    Kamtschatka, you should say your real requirement earlier J.

    CodeModel could resolve your issue.

    Please see this link

    Discovering Code by Using the Code Model (Visual C#).

    You could get the Attributes of your class or your method using CodeClass.Attributes Property or CodeFunction.Attributes Property. And set the attribute value by setting CodeAttribute.Value Property.

    Thanks

    Chao

    Thursday, April 1, 2010 3:01 AM
  • Hi

    That is only the first part of my requirement and i already knew how to do that, but how can i select "true" after i set the CodeAttribute.Value Property?

    Let's say i already generated the Attribute Value(either by EditPoint or CodeAttribute.Value) how to select a small part of a line of code. As far as i found out, it is only possible to Select either the whole line or the whole document, which is not what i want to do. I would need a method to set the Start and End Point of a selection.

     

    Thanks

    Thursday, April 1, 2010 8:04 AM
  • Hello, kamtschatka

    Please research the codeModel carefully, and the links I have given to you. I am sure the CodeModel could satisfy your needs.

    I believe you didn’t refer the links I provide you, if you want to change the attribute using codeModel, you won’t need to select the document or a line anymore, you could find all the code elements (like class, function, property, attribute) in the document, and modify these code elements.

    When you find the codeAttribute of your custom attribute, you could simply change the value like

    codeAttribute.value = “\"name\", Value1=true””;

    If you don’t study or research the codeModel, you won’t get an answer.

    Thanks

    Chao

    Friday, April 2, 2010 2:29 AM
  • Hi

    Sorry, but i have to make it very clear: I know how to change the Value and that is not the problem! I WANT to select the word "true". It should be very similar to the CodeSnippet functionality: Insert a Snippet and select the word, so the user is able to override it, if it's not the correct value.(Please don't suggest using Snippets, because i already asked in another Thread, if it is possible to dynamically generate Snippets-->it's not)

    Please stop telling me how to generate code or that i have to use the CodeModel!

    As I said in the first post: I want to select a particular part of the code, not the whole line or the whole document, in my case i would like to select 4 chars("true").

    So this is what i want to do Step-by-Step:

    1. Generate the String "Value1=true" into the Code (For this i will either use an EditPoint or the CodeModel - THIS IS NOT THE PROBLEM!)

    2. Select "true" from the previous step(You posted the link to the TextSelection Interface and it only contains SelectLine and SelectDocument)

    Thanks

    Friday, April 2, 2010 9:38 AM
  • So is this possible from an AddIn or do i have to use ITextSelection.Select Method ? If i have to use ITextSelection, is there any information how to do this?  I would have to do this in a Button Click event, how would i retrieve all the references to perform this action?
    Thursday, April 8, 2010 1:13 AM
  • Ok i finally found it out on my own. Turns out, that my first try was correct, but there seems to be a bug in the SDK.

    //This line selects the 4 characters to the right of the current caret position
    //The Text is selected correctly, but the active Window loses the focus-->if the user types in something, nothing happens
                    selection.CharRight(true,4);
    //This line activates the active document(a bit strange, but it works).
                    application.ActiveDocument.Activate();

     

    Maybe this only occurs in the RC1?

    • Marked as answer by kamtschatka Tuesday, April 13, 2010 8:26 PM
    Tuesday, April 13, 2010 8:26 PM