kubaPod / DevTools

DevTools and styles for mathematica developers
28 stars 6 forks source link

New action: LocalizeVariable #7

Closed kubaPod closed 4 years ago

kubaPod commented 4 years ago

Incorporate:

<|"Label" -> "Localize variable", 
  "ShortKey" -> "l", 
  "Action" :> Catch @ Module[
      {initialCaretPosition, symbol, selection, nb=InputNotebook[], modified = False, SymbolNameQ}
    , SymbolNameQ = StringMatchQ[LetterCharacter~~(LetterCharacter|DigitCharacter|"$")...] 

    ; symbol = CurrentValue@"SelectionData"
    ; If[ !MatchQ[symbol, _String?SymbolNameQ], Throw @ $Failed]
    ; initialCaretPosition = "CharacterRange" /. FrontEndExecute@FrontEnd`UndocumentedGetSelectionPacket@nb
    ; While[
        True
      , If[selection===$Failed, Break[]]
      ; If[
          Not @ MatchQ[selection,RowBox[{"Module"|"With"|"Block"|"DynamicModule","[",___}]]
        , FrontEndExecute @ FrontEndToken[nb,"ExpandSelection"]
        ; selection = CurrentValue@"SelectionData"
        ; Continue[]
       ]
      ; SelectionMove[nb,Before,Word] (*before head*)
      ; SelectionMove[nb,After,Word,3] (*after  head[{ *)
      ; NotebookWrite[nb, symbol<>", "]
      ; modified=True
      ; Break[]
      ]
    ; SelectionMove[nb, Before, CellContents]
    ; SelectionMove[nb, Next, Character, First @ initialCaretPosition + StringLength[symbol<>", "]If[modified, 1 , 0]  ]  
    ; SelectionMove[nb, All, Character, #2-#& @@ initialCaretPosition]
  ]

|>