使用IDE宏遍历代码中的非ASCII字符

    在产品开发的时候,虽然大家知道需要做I18N的支持。不过在开发代码的过程中,由于需要首先关注逻辑实现,所以文件中也难免会hardcode中文文字什么的。在代码的UI描述和业务逻辑都很稳定后,最后还需要清理一下代码中是否还有中文资源,因为那些类似错误提示一类的描述,在程序未出错时Tester几乎是无法发现的。

    不过这个检查如果靠眼睛看,也是非常之郁闷的事情,那么除了编写额外的工具分析文档,还有没有其它简单又自动的方法帮我们解决问题呢?由于VS.NET 2003给我提供了非常强大的宏功能(可以在宏中使用.NET Framework类库!),所以对于这种小case使用宏来解决就太合适不过了。下面这个宏的功能就是在当前文档中找出非ASCII字符,并把出现的行号和内容输出到Output工具窗口中。代码如下:
None.gif Imports  EnvDTE
None.gif
Imports  System.Windows.Forms
None.gif
Imports  System.Text
None.gif
Imports  System.Globalization
None.gif
Imports  System.Text.RegularExpressions
None.gif
Imports  System.Diagnostics
None.gif
Imports  MyMacros.Util
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Public   Module Birdshome Module Birdshome
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Sub FindChineseWords()Sub FindChineseWords()
InBlock.gif        
Dim doc As Document = DTE.ActiveDocument
InBlock.gif        
Dim docText As TextDocument = doc.Object
InBlock.gif        
Dim lineCount = docText.EndPoint.Line
InBlock.gif        
Dim ep As EditPoint = docText.StartPoint.CreateEditPoint()
InBlock.gif        
Dim strLine As String
InBlock.gif        
Dim i As Integer
InBlock.gif        
Dim regex As Regex = New Regex("[^\u0000-\u00ff]+")
InBlock.gif        
Dim strbResult As StringBuilder = New StringBuilder
InBlock.gif        
For i = 1 To lineCount - 1
InBlock.gif            strLine 
= ep.GetLines(i, i + 1)
InBlock.gif            
Dim m As MatchCollection = regex.Matches(strLine)
InBlock.gif            
If m.Count > 0 Then
InBlock.gif                strbResult.Append(i.ToString())
InBlock.gif                strbResult.Append(
"")
InBlock.gif                
Dim j As Integer
InBlock.gif                
For j = 0 To m.Count - 1
InBlock.gif                    strbResult.Append(m(j).Value)
InBlock.gif                    strbResult.Append(
"")
InBlock.gif                
Next
InBlock.gif                strbResult.Length 
= strbResult.Length - 2
InBlock.gif                strbResult.Append(
";")
InBlock.gif                strbResult.Append(System.Environment.NewLine)
InBlock.gif            
End If
InBlock.gif        
Next
InBlock.gif        
'MessageBox.Show(strbResult.ToString)
InBlock.gif
        Dim win As Window = DTE.Windows.Item(Constants.vsWindowKindCommandWindow)
InBlock.gif        
Dim target As Object
InBlock.gif        
If (DTE.ActiveWindow Is win) Then
InBlock.gif            target 
= win.Object
InBlock.gif        
Else
InBlock.gif            target 
= GetOutputWindowPane("Chinese Words")
InBlock.gif            target.clear()
InBlock.gif        
End If
InBlock.gif        target.OutputString(strbResult.ToString())
ExpandedSubBlockEnd.gif    End Sub

ExpandedBlockEnd.gifEnd Module

    方法GetOutputWindowPane在如下公用模块中:
None.gif Imports  EnvDTE
None.gif
Imports  System.Diagnostics
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Public   Module Util Module Util
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Function GetOutputWindowPane()Function GetOutputWindowPane(ByVal Name As StringOptional ByVal show As Boolean = TrueAs OutputWindowPane
InBlock.gif        
Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
InBlock.gif        
If show Then win.Visible = True
InBlock.gif        
Dim ow As OutputWindow = win.Object
InBlock.gif        
Dim owpane As OutputWindowPane
InBlock.gif        
Try
InBlock.gif            owpane 
= ow.OutputWindowPanes.Item(Name)
InBlock.gif        
Catch e As System.Exception
InBlock.gif            owpane 
= ow.OutputWindowPanes.Add(Name)
InBlock.gif        
End Try
InBlock.gif        owpane.Activate()
InBlock.gif        
Return owpane
ExpandedSubBlockEnd.gif    
End Function

ExpandedBlockEnd.gif
End Module
// 多写写VB,觉得它的语法还是挺自然的:)

    以中文google首页的html代码(Formatted by IDE)为示例,运行本脚本代码的结果如下图:
    ChineseWords.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值