vs_integration/src/Commands.cpp
//------------------------------------------------------------------------------
// This function fills file name, line number and ending column number
// for the selected token as well as a token itself.
//------------------------------------------------------------------------------
This function fills file name, line number and ending column number
for the selected token as well as a token itself.
This function fills file name line number and ending column number for the selected token as well as a token itself .
bool CCommands::getSelection(CComBSTR& file, CComBSTR& token, long& offset, long& line, long& column) {
ITextDocument* iDocument;
ITextSelection* iSelection;
	if(m_pApplication->get_ActiveDocument((IDispatch **)&iDocument)!=S_OK) 
		return false;
	if(iDocument==NULL) return false;
	BSTR bstrName;
	iDocument->get_FullName(&bstrName);
	file=bstrName;
	if(iDocument->get_Selection((IDispatch **)&iSelection)!=S_OK) 
		return false;
	iDocument->Release();
	if(iSelection==NULL) return false;
	BSTR bstrToken;
	if(iSelection->get_Text(&bstrToken)!=S_OK) 
		return false;
	token = bstrToken;
	long size = token.Length();
	if(iSelection->get_TopLine(&line)!=S_OK)     return false;
	if(iSelection->get_CurrentColumn(&column)!=S_OK) return false;
	VARIANT selMove;
	selMove.vt = VT_INT;
	selMove.intVal = dsMove;
	VARIANT selExtend;
	selExtend.vt = VT_INT;
	selExtend.intVal = dsExtend;
	VARIANT repCount;
	repCount.vt = VT_INT;
	repCount.intVal = 1;
	bool cursor_at_right_end = true;
	if (column <= 1)
	{
		cursor_at_right_end = false;
	}
	else
	{
		long newsize;
		iSelection->CharLeft(selExtend, repCount);
		if(iSelection->get_Text(&bstrToken)==S_OK) 
		{
			CComBSTR newToken = bstrToken;
			newsize = newToken.Length();
			if (newsize > size)
			{
				cursor_at_right_end = false;
			}
		}
		iSelection->CharRight(selExtend, repCount);
	}
	long lineOffset = 0;
	long tempColumn = column;
	while (tempColumn > 1) {
		iSelection->CharLeft(selMove, repCount);
		iSelection->get_CurrentColumn(&tempColumn);
		lineOffset++;
	}
	if (cursor_at_right_end)
	{
		iSelection->MoveTo(line, column-size, selMove);
	}
	else 
	{
		iSelection->MoveTo(line, column+size, selMove);
	}
	iSelection->MoveTo(line, column, selExtend);
	iSelection->Release();
	column = lineOffset;
	if(size!=0)
		column--;
	offset = -1; 
	return true;
}
bool CCommands :: getSelection ( CComBSTR & file , CComBSTR & token , long & offset , long & line , long & column ) { ITextDocument * iDocument ; ITextSelection * iSelection ; if ( m_pApplication -> get_ActiveDocument ( ( IDispatch * * ) & iDocument ) != S_OK ) return false ; if ( iDocument == NULL ) return false ; BSTR bstrName ; iDocument -> get_FullName ( & bstrName ) ; file = bstrName ; if ( iDocument -> get_Selection ( ( IDispatch * * ) & iSelection ) != S_OK ) return false ; iDocument -> Release ( ) ; if ( iSelection == NULL ) return false ; BSTR bstrToken ; if ( iSelection -> get_Text ( & bstrToken ) != S_OK ) return false ; token = bstrToken ; long size = token . Length ( ) ; if ( iSelection -> get_TopLine ( & line ) != S_OK ) return false ; if ( iSelection -> get_CurrentColumn ( & column ) != S_OK ) return false ; VARIANT selMove ; selMove . vt = VT_INT ; selMove . intVal = dsMove ; VARIANT selExtend ; selExtend . vt = VT_INT ; selExtend . intVal = dsExtend ; VARIANT repCount ; repCount . vt = VT_INT ; repCount . intVal = 1 ; bool cursor_at_right_end = true ; if ( column <= 1 ) { cursor_at_right_end = false ; } else { long newsize ; iSelection -> CharLeft ( selExtend , repCount ) ; if ( iSelection -> get_Text ( & bstrToken ) == S_OK ) { CComBSTR newToken = bstrToken ; newsize = newToken . Length ( ) ; if ( newsize > size ) { cursor_at_right_end = false ; } } iSelection -> CharRight ( selExtend , repCount ) ; } long lineOffset = 0 ; long tempColumn = column ; while ( tempColumn > 1 ) { iSelection -> CharLeft ( selMove , repCount ) ; iSelection -> get_CurrentColumn ( & tempColumn ) ; lineOffset ++ ; } if ( cursor_at_right_end ) { iSelection -> MoveTo ( line , column - size , selMove ) ; } else { iSelection -> MoveTo ( line , column + size , selMove ) ; } iSelection -> MoveTo ( line , column , selExtend ) ; iSelection -> Release ( ) ; column = lineOffset ; if ( size != 0 ) column -- ; offset = -1 ; return true ; }
This function fills file name, line number and ending column number
for the selected token as well as a token itself.
This function fills file name line number and ending column number for the selected token as well as a token itself .
// Getting the currently opened document object. // Reading the file name // Getting text selection object from the active document // We do not need active document object any more. // Reading currently selected token // To determine which end of the selection the cursor is at, // we extend the selection one character left from the cursor  // and see whether the selection has gotten longer or shorter. // If extending left increases the size, the // cursor must be at the left end. // Calculating column in file characters, not in screen // position (detecting tabs) // Restoring selection after calculation: // First, go to the end without the cursor // Cursor at left end of selection // Now make the selection // can not determine the offset