Re: Unicode setting question
"David Ching" <dc@remove-this.dcsoft.com> ha scritto nel messaggio
news:dHP%j.5866$Ri.2447@flpi146.ffdc.sbc.com...
"Mihai N." <nmihai_year_2000@yahoo.com> wrote in message
news:Xns9AAE10B6B7B7MihaiN@207.46.248.16...
You can have the macro go left from cursor and find the beginning of the
string, then right and find the end. then you can be anywhere in the
string.
Or you can make it work when you are done with the string, in which case
the
full string is at left, just do a bit of parsing:
"Hello worl<cursor>
"Hello world<cursor>
"Hello world"<cursor>
"Hello world"<cursor>Ctrl-Whatever
_T("Hello world")<cursor>
The best I ever came to a solution was to define a Visual Assist autotext
template assigned to the 'T' key. When I typed (T + Space), it expanded
to
_T("")
and put the cursor between the quotes. That still left me having to type
Ctrl+End to move out of teh quotes after I was doine typing the string,
though.
I wrote a macro for VS2008 (I hope it works for older IDEs, too) to make
things a little better.
It works in both cases:
"ciao"<cursor>
"ciao<cursor>"
and moves your cursor after the closing )
If you assing to a short-cut like CTRL+<something> it may help a bit...
It decorates using _T()
<code>
Imports System
Imports EnvDTE
Imports System.Diagnostics
Public Module UnicodeTDecorator
'--------------------------------------------------------------------------
' Macro to decorate an undecorated string using _T().
'
' By Giovanni Dicanio
' 2008, May 30th
'
' Position the cursor on the left or on the right of the closing " of
' the string to decorate, and then run this macro.
'
' Valid cursor positions:
'
' "ciao"<cursor>
' "ciao<cursor>"
'
'
' Supports UNDO.
'
'--------------------------------------------------------------------------
Public Sub UnicodeTDecorate()
' The quote " character
Const QuoteChar As Char = Chr(34)
' Access current cursor position (EditPoint)
Dim ts As TextSelection = DTE.ActiveDocument.Selection
Dim ep As EditPoint = ts.ActivePoint.CreateEditPoint
' This will point to the closing "
Dim epEnd As EditPoint = ep.CreateEditPoint
' Error Message-box Title
Dim errorTitle As String = "[Unicode String Decorator] *** ERROR
***"
Try
' This macro can work in both cases of cursor
' on the left or on the right of closing quote:
'
' "ciao"<cursor>
' "ciao<cursor>"
'
' Check that the cursor is on the right of closing "
If (epEnd.GetText(-1) <> QuoteChar) Then
' Check that the cursor is on the left of closing "
If (epEnd.GetText(1) <> QuoteChar) Then
' Bad position
MsgBox( _
"Cursor is in bad position. " & vbCrLf & _
"The cursor must be on the left or on" & vbCrLf & _
"the right of the ending string quote.", _
MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, _
errorTitle)
Exit Sub
End If
Else
' The cursor is on the right of closing " .
'
' Move a position back, we want it on the
' left of " to start algorithm
epEnd.CharLeft()
End If
' Debug:
' Write(" Character at curr pos = " & epEnd.GetText(1))
' Move leftwards until we find matching " (the opening ")
Dim openQuoteFound As Boolean = False
Dim epStart As EditPoint = epEnd.CreateEditPoint
While (Not epStart.AtStartOfLine)
' Move one position on the left
epStart.CharLeft()
' If we found " , it is the opening " ,
' so we can stop looping
If (epStart.GetText(1) = QuoteChar) Then
openQuoteFound = True
Exit While
End If
End While
' Check that we exited from the loop because we found
' the opening ", and not for begin-of-line condition
If openQuoteFound = False Then
' No matching opening " found
MsgBox( _
"No opening string quote found.", _
MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, _
errorTitle)
Exit Sub
End If
' We have just found where the string begins, and where it ends.
' Debug
' Write("Opening quote at : " & epStart.LineCharOffset & " " &
epStart.GetText(1))
' Write("Closing quote at : " & epEnd.LineCharOffset & " " &
epEnd.GetText(1))
' Support for Undo
Dim undoWasOpen As Boolean = False
If DTE.UndoContext.IsOpen = True Then
undoWasOpen = True
Else
' Extract the the string we are decorating
epStart.CharRight()
Dim stringToDecorate As String = epStart.GetText(epEnd)
epStart.CharLeft()
Dim undoContextName As String
undoContextName = "Unicode String Decoration : " & QuoteChar
& _
stringToDecorate & QuoteChar & _
" (line: " & epStart.Line & ")"
' Debug
' Write(undoContextName)
DTE.UndoContext.Open(undoContextName, False)
End If
'
' *** Insert Unicode _T() decoration ***
'
epStart.Insert("_T(")
epEnd.CharRight()
epEnd.Insert(")")
' Move after the closing )
ts.MoveToPoint(epEnd)
' Close the undo context only if it has been opened by us here
If undoWasOpen = False Then
' Close the undo object to commit changes
DTE.UndoContext.Close()
End If
' *** ALL RIGHT *** !
Catch ex As Exception
' Display any exception error message ...
MsgBox( _
ex.Message, _
MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, _
errorTitle)
End Try
End Sub
'==========================================================================
' Helpers for Output/Log
' May be useful during debugging and development too
'==========================================================================
'--------------------------------------------------------------------------
' Name: Write
' Desc: Write a "log" line
'--------------------------------------------------------------------------
Private Sub Write(ByVal s As String)
Dim out As OutputWindowPane = GetOutputWindowPane("MACRO OUTPUT",
True)
out.OutputString(s)
out.OutputString(vbCrLf)
End Sub
Private Function GetOutputWindowPane(ByVal Name As String, _
Optional ByVal show As Boolean = True) _
As OutputWindowPane
Dim win As Window =
DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
If show Then win.Visible = True
Dim ow As OutputWindow = win.Object
Dim owpane As OutputWindowPane
Try
owpane = ow.OutputWindowPanes.Item(Name)
Catch e As System.Exception
owpane = ow.OutputWindowPanes.Add(Name)
End Try
owpane.Activate()
Return owpane
End Function
End Module
</code>
HTH,
Giovanni