NigelThorne (owner)

Revisions

gist: 143346 Download_button fork
public
Description:
Helper macro to be used with StoryQ
Public Clone URL: git://gist.github.com/143346.git
Embed All Files: show embed
StringToLambda #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
 
Public Module Module1
 
    Sub StoryQConvert()
        Dim textSelection As String
 
        DTE.UndoContext.Open("StoryQConvert")
        textSelection = DTE.ActiveDocument.Selection.Text
 
        If (textSelection.Trim.StartsWith("""")) Then
            StringToLambda()
        Else
            LambdaToString()
        End If
        DTE.UndoContext.Close()
    End Sub
    Sub StringToLambda()
        Dim textSelection As TextSelection
        Dim output As String
        Dim capitalizeNext As Boolean
 
        textSelection = DTE.ActiveDocument.Selection
 
        capitalizeNext = True
        For Each character In textSelection.Text
            If (character = " " Or character = """") Then
                capitalizeNext = True
            Else
                If capitalizeNext Then
                    output = output + Char.ToUpper(character)
                    capitalizeNext = False
                Else
                    output = output + character
                End If
            End If
        Next
        textSelection.Text = "() => " + output + "()"
    End Sub
 
 
    Sub LambdaToString()
        Dim textSelection As TextSelection
        Dim output As String
        textSelection = DTE.ActiveDocument.Selection
 
        For Each character In textSelection.Text
            If ("() =>".Contains(character)) Then
                Continue For
            End If
            If (Char.ToUpper(character) = character) Then
                output = output + " "
            End If
            output = output + Char.ToLower(character)
        Next
        textSelection.Text = """" + output.TrimStart + """"
    End Sub
End Module