Technical Info
- Visual Studio SDK Version: 2013
- Windows Version: 8.1 x64
- Development Language: VB.Net and also C#
Question
How I could get and replace the current word in the code editor? I mean the full word at where the caret is, not the selection text.
This is a pseudo-code of what I try to do:
Dim caret As ITextCaret = Tools.GetCurrentViewHost.TextView.Caret Dim span As Span = caret.ContainingTextViewLine.Extent.Span Dim snapshot As ITextSnapshot = caret.ContainingTextViewLine.Extent.Snapshot Dim word As String = ...? snapshot.TextBuffer.Replace(span, word & "some text"))
Research
I've tried to figure it out researching the members of Microsoft.VisualStudio.Text.Editor.Caret without success.
I also seen a random code in MSDN which I'm not sure if it is related with this issue, anyways I cannot figure out what I should use asm_provider object:
https://msdn.microsoft.com/en-us/library/ff926100.aspx?f=255&MSPPError=-2147217396
'neither an expansion session nor a completion session is open, but we got a tab, so check whether the last word typed is a snippet shortcut If m_session Is Nothing AndAlso m_exSession Is Nothing AndAlso nCmdID = CUInt(VSConstants.VSStd2KCmdID.TAB) Then 'get the word that was just added Dim pos As CaretPosition = m_textView.Caret.Position Dim word As TextExtent = m_provider.NavigatorService.GetTextStructureNavigator(m_textView.TextBuffer).GetExtentOfWord(pos.BufferPosition - 1) Dim textString As String = word.Span.GetText() 'if it is a code snippet, insert it, otherwise carry on If InsertAnyExpansion(textString, Nothing, Nothing) Then Return VSConstants.S_OK End If End If