none
Getting TextSelection thru SelectedItem RRS feed

  • Question

  • I have question, I can get TextSelection form ActiveDocument->Selection and now, I will get thru SelectedItem.

    Active Document way

    TextSelection^ TxtSel = (TextSelection^)m_DTE->ActiveDocument->Selection;

    TxtSel->StartOfDocument(false); while (TxtSel->ActivePoint->AtEndOfDocument != true) { TxtSel->SelectLine();

    but thru SelectedItem it's return exception

    OleMenuCommand^ MenuItem = (OleMenuCommand^)sender;
    bool IsCompatible = false;
    				
    if (m_DTE->Solution->FullName != "")
    {
       int index = 1;
       while (index <= m_DTE->SelectedItems->Count && index != -1)
       {
    	SelectedItem^ selItem = m_DTE->SelectedItems->Item(index);
    						
    	if (selItem->Name->EndsWith(".h"))
    	{	
       	    String^ FullName = selItem->ProjectItem->FileNames[0];
    	    TextSelection^ TxtSel = (TextSelection^)selItem->ProjectItem->Document->Selection;
    

    How reach TextSelection from SelectedItem

    Thanks.


    • Edited by Fleµve Monday, September 25, 2017 3:30 PM
    Monday, September 25, 2017 3:29 PM

Answers

All replies

  • A Document represents a file on memory, inside a Window, and the window can be visible or not, but in either case there is a document, a text selection, etc.

    A ProjectItem represents a file on disk. If the file is not opened (no window), then ProjectItem.Document returns null.

    You can open an invisible Document from a ProjectItem using:

    Window window = projectItem.Open(view);

    // window.Visible = true;

    // Do something using window.Document or projectItem.Document

    window.Close(...)

    See:

    HOWTO: Manipulate a code or form designer file from a Visual Studio add-in

    http://www.visualstudioextensibility.com/articles/add-ins/


    My portal and blog about VSX: http://www.visualstudioextensibility.com; Twitter: https://twitter.com/VSExtensibility; MZ-Tools productivity extension for Visual Studio: https://www.mztools.com

    Tuesday, September 26, 2017 10:08 AM
    Moderator
  • You can open an invisible Document from a ProjectItem using:

    Window window = projectItem.Open(view);

    // window.Visible = true;

    // Do something using window.Document or projectItem.Document

    window.Close(...)

    Thanks it's exactly what I understand late last night.  I've also try a projectItem->NewWindow() before, but create windows popup glitch (it's a VSIX context menu before query callback). I never understand how use projectItem->Open before, so thanks again.

    I get confuse at the beginning with ProjectItem->Open(System::String^ ViewKind). Like NewWindow->Close(EnvDTE::vsSaveChanges SaveChanges) asking for a enum value, I taking some time before realize that I must use Constants::vsViewKindCode instead "vsViewKindCode".

    Now it's work perfectly, have to change some code because it's slow a little the context menu. But I've a esthetic glitch about already open file. I save Active point before if windows already open, but after it's replace with a 1-2 line offset (scroll bar) and give illusion that document have move. I've also try SetBookmark and give same illusion.

    void FastLibFuncQueryCallback(Object^ sender, EventArgs^ e)
    {
    	OleMenuCommand^ MenuItem = (OleMenuCommand^)sender;
    
    	if (m_DTE->Solution->FullName != "")
    	{
    		int index = 1;
    		while (index <= m_DTE->SelectedItems->Count && index != -1)
    		{
    			SelectedItem^ selItem = m_DTE->SelectedItems->Item(index);
    			//if (!selItem->Name->EndsWith(".h")) //New Way
    			//index = -1;
    
    			// move start
    			if (selItem->Name->EndsWith(".h")) //Must be transfert to FastLibFunc... create slow inside context menu
    			{
    				bool AreOpen = selItem->ProjectItem->IsOpen[selItem->Name];
    				Window^ NewWindow = nullptr;
    
    				if (!AreOpen)
    					NewWindow = selItem->ProjectItem->Open(Constants::vsViewKindCode);
    
    				TextSelection^ TxtSel = (TextSelection^)selItem->ProjectItem->Document->Selection;
    				bool haveHIdent = false;
    				bool haveCPPIdent = false;
    				EditPoint^ TxtPos;
    
    				if (AreOpen)
    					TxtPos = TxtSel->ActivePoint->CreateEditPoint();
    
    				TxtSel->StartOfDocument(false);
    				while (!TxtSel->ActivePoint->AtEndOfDocument && (!haveCPPIdent || !haveHIdent))
    				{
    					TxtSel->SelectLine();
    					if (TxtSel->Text->Contains("#ifndef"))
    					{
    						if (TxtSel->Text->Contains("_H__")) haveHIdent = true;
    						if (TxtSel->Text->Contains("_CPP__")) haveCPPIdent = true;
    					}
    				}
    
    				if (!haveCPPIdent || !haveHIdent)
    					index = -1;
    
    				if (!AreOpen)
    					NewWindow->Close(vsSaveChanges::vsSaveChangesNo);
    				else
    					TxtSel->MoveTo(TxtPos->Line, TxtPos->DisplayColumn, false);
    			}
    			else
    				index = -1;
    			// move end
    
    			if (index != -1)
    				index++;
    		}
    
    		MenuItem->Enabled = index == -1 ? false : true;
    	}
    
    	MenuItem->MatchedCommandId = 0;
    }
    
    void FastLibFunc(Object^ sender, EventArgs^ e)
    {
    	for (int i = 1; i <= m_DTE->SelectedItems->Count; i++)
    	{
    		SelectedItem^ selItem = m_DTE->SelectedItems->Item(i);
    		String^ msg = "The first selected item is " + selItem->Name;
    		msg += "\nThis SelectedItem corresponds to the following ProjectItem: " + selItem->ProjectItem->FileNames[0];
    		msg += "\nThis SelectedItem is part of a collection of " + selItem->Collection->Count.ToString() + " items.";
    		msg += "\nThis is contained in the following application: " + selItem->DTE->Name;
    		msg += "\nDocument Type" + selItem->ProjectItem->Object->GetType()->FullName;
    		MessageBox::Show(msg);
    	}
    
    	String^ config = ((VCProject^)m_DTE->Solution->Projects->Item(1)->Object)->ActiveConfiguration->Evaluate("$(VC_ExecutablePath_x64_x64)");
    	
    }

    Thanks.

    Tuesday, September 26, 2017 4:40 PM