1 people like it.

Convert the number of indentation spaces in a sourcecode.

This small script converts continuous 4 spaces to 2 spaces by using Regex. It gets the source code from a clip board text and save it to clip board. Since this program doesn't parse the source code, conversion is not perfect. But writing script which interacts with clipboard is so easy, you can automate your trivial coding work with a small effort. Using F#interactive with Clipboard is my favorite way of meta-programming.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
#r "WindowsBase"
#r "PresentationCore"
open System
open System.Text.RegularExpressions
open System.Windows
let indentConverter before after =
  let spaces n = String(' ',n)
  Regex.Replace(Clipboard.GetText(), spaces before, spaces after)
  |> Clipboard.SetText
indentConverter 4 2
namespace System
namespace System.Text
namespace System.Text.RegularExpressions
namespace System.Windows
val indentConverter : before:int -> after:int -> unit

Full name: Script.indentConverter
val before : int
val after : int
val spaces : (int -> String)
val n : int
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
Multiple items
type Regex =
  new : pattern:string -> Regex + 1 overload
  member GetGroupNames : unit -> string[]
  member GetGroupNumbers : unit -> int[]
  member GroupNameFromNumber : i:int -> string
  member GroupNumberFromName : name:string -> int
  member IsMatch : input:string -> bool + 1 overload
  member Match : input:string -> Match + 2 overloads
  member Matches : input:string -> MatchCollection + 1 overload
  member Options : RegexOptions
  member Replace : input:string * replacement:string -> string + 5 overloads
  ...

Full name: System.Text.RegularExpressions.Regex

--------------------
Regex(pattern: string) : unit
Regex(pattern: string, options: RegexOptions) : unit
Regex.Replace(input: string, pattern: string, evaluator: MatchEvaluator) : string
Regex.Replace(input: string, pattern: string, replacement: string) : string
Regex.Replace(input: string, pattern: string, evaluator: MatchEvaluator, options: RegexOptions) : string
Regex.Replace(input: string, pattern: string, replacement: string, options: RegexOptions) : string
type Clipboard =
  static member Clear : unit -> unit
  static member ContainsAudio : unit -> bool
  static member ContainsData : format:string -> bool
  static member ContainsFileDropList : unit -> bool
  static member ContainsImage : unit -> bool
  static member ContainsText : unit -> bool + 1 overload
  static member GetAudioStream : unit -> Stream
  static member GetData : format:string -> obj
  static member GetDataObject : unit -> IDataObject
  static member GetFileDropList : unit -> StringCollection
  ...

Full name: System.Windows.Clipboard
Clipboard.GetText() : string
Clipboard.GetText(format: TextDataFormat) : string
Clipboard.SetText(text: string) : unit
Clipboard.SetText(text: string, format: TextDataFormat) : unit
Raw view New version

More information

Link:http://fssnip.net/8Z
Posted:5 years ago
Author:nagat01
Tags: meta-programming , scripting