Page 1 of 1

Feature Request: Remove empty lines

Posted: Sun Oct 24, 2010 7:03 am
by IceSoft
Feature Request: Remove empty lines

Or is there an menu entry which I have not seen?

Thanks
Icesoft

Re: Feature Request: Remove empty lines

Posted: Mon Oct 25, 2010 4:25 pm
by Rook Zimbabwe
and REM lines... if possible... that would be good...

Re: Feature Request: Remove empty lines

Posted: Fri Oct 29, 2010 4:40 am
by Mistrel
Wait.. what? :|

You want your code to be sandwiched into a wall of text?

Re: Feature Request: Remove empty lines

Posted: Fri Oct 29, 2010 7:07 am
by PB
They could have commented lines as the separators. ;)

Re: Feature Request: Remove empty lines

Posted: Fri Oct 29, 2010 8:03 pm
by utopiomania
I use empty lines to make my sourcecode more readable. If the compiler or the exe don't
mind why remove them?

Re: Feature Request: Remove empty lines

Posted: Fri Oct 29, 2010 9:23 pm
by Michael Vogel
Hm, maybe there are different opinions about readable code, I love some empty lines within procedures, but don't like them seperating procedures, because a typical program will need a lot of scrolling even when all procedures are collapsed.

Therefore I personally like to have all comments for a procedure right/below the the first procedure line, so it will also be collapsable.

However, here's a fast possibility to get rid of empty lines - it's a quick and dirty code for now, but if it is useful, I could optimize it for recognizing "Procedure " and "Procedure." but not lines like "ProcedureVar=1". The same should work for "Macros" also...

But one after the other, here's the simple code which should be compiled as "Auto Compactor.exe" and copied into the "Catalog" directory of PureBasic. After that, just add is as a tool, using [%COMPILEFILE\..\Catalogs\Tool AutoCompactor.exe] for the command line, ["%TEMPFILE" %SELECTION] as arguments and [%COMPILEFILE\..] as the working directory (don't add the brackets).
Just activate also the options "wait until tool quits" and "reload source after tool has quit", that's it.

Code: Select all

;{ AutoCompactor by Michael Vogel Vo.o1}

    ; INSTALLATION:
    ; copy compiled exe into the directory "...\Purbasic\Catalogs"
    ; install the tool using the parameter "%TEMPFILE" %SELECTION
    ; use the PureBasic as the working directory (or adapt the OpenPreferences path below)
    ; Select 'Wait until tool quits' And 'Reload source after tool has quit'

    ;    %COMPILEFILE\..\Catalogs\Tool AutoIndent.exe
    ;    "%TEMPFILE" %SELECTION
    ;    %COMPILEFILE\..
    ;    Auto &Indent
    ;    Menu Or Shortcut

    ;     × Wait until tool quits
    ;     × Reload Source after tool has quit
    ;     × into the current source


    ; DESCRIPTION:
    ; With pressed shift key:        remove all empty lines
    ; without shift key:            just remove lines between endprocedure/procedure
    ; no selection:                    complete source code is affected
    ; selection of lines:            only selected lines are changed


    ; HISTORY & ISSUES:
    ; first beta

;}

; Define

; EndDefine

Procedure.s RightTrim(s.s)
    l.l=Len(s)
    While l
        l-1
        b.l=PeekB(@s+l)
        If b<>9 And b<>32
            Break
        EndIf
    Wend
    ProcedureReturn Left(s,l+1)
EndProcedure
Procedure.s LeftTrim(s.s)
    l.l=Len(s)
    c.l=0
    While c<l
        b.l=PeekB(@s+c)
        If b<>9 And b<>32 ; Space & Tab
            Break
        EndIf
        c+1
    Wend
    ProcedureReturn Mid(s,c+1,l-c)
EndProcedure

Procedure Init()

    ; Dateinamen holen...
    CompilerIf 1
        If CountProgramParameters()<>2
            MessageBox_(0,"'AutoCompactor' benötigt Parameter!"+#CR$+"(%TEMPFILE und %SELECTION)","Fehler",#MB_ICONERROR| #MB_OK)
            End
        EndIf
        Global OutputFile.s=ProgramParameter()
    CompilerElse
        Global OutputFile.s="Test.pb"
    CompilerEndIf

    Global InputFile.s=OutputFile+"~"
    DeleteFile(InputFile)
    If RenameFile(OutputFile,InputFile)=0
        MessageBox_(0,"'AutoCompactor' konnte keine Temporärdatei erstellen","Fehler",#MB_ICONERROR| #MB_OK)
        End
    EndIf

    ; Selektion abfragen...
    Global zeile.l=0
    selektion.s=ProgramParameter()
    Global Startzeile.l=Val(StringField(selektion, 1, "x"))
    Global Endzeile.l=Val(StringField(selektion, 3, "x"))

    ;CreateFile(3,"c:\debug.log")
    ;WriteStringN(3,Str(Startzeile))
    ;WriteStringN(3,Str(Endzeile))
    ;CloseFile(3)

    If Startzeile>=Endzeile
        Startzeile=1
        Endzeile=#MAXSHORT
    EndIf

EndProcedure
Procedure Doit(mode.l)

    ; Doit(0)    standard compacting
    ; Doit(1)    complete compacting

    ;CreateFile(3,"c:\debug.log")
    ;WriteStringN(3,"s "+FormatDate("%hh:%ii:%ss", Date())+#TAB$+Str(GetTickCount_()))

    Init()

    ReadFile(1,InputFile)
    CreateFile(2,OutputFile)

    Protected RemoveFlag=mode
    Protected Content.s

    While Not(Eof(1))
        z.s=ReadString(1)
        zeile.l+1

        z=righttrim(z)        ; rechts kann man immer säubern

        If zeile>=Startzeile And zeile<=Endzeile

            Content=LCase(lefttrim(z))

            If mode
                RemoveFlag=#True
            ElseIf Left(Content,9)="procedure"
                RemoveFlag=#False
            ElseIf Left(Content,12)="endprocedure"
                RemoveFlag=#True
            EndIf

            If (RemoveFlag=#False) Or Len(Content)
                WriteStringN(2,z)
            EndIf
            
        Else
            
            WriteStringN(2,z)
            
        EndIf ;

    Wend
    CloseFile(1)
    CloseFile(2)

    ;WriteStringN(3,"e "+FormatDate("%hh:%ii:%ss", Date())+#TAB$+Str(GetTickCount_()))
    ;CloseFile(3)

EndProcedure

Doit((GetKeyState_(#VK_SHIFT)&$80))


Re: Feature Request: Remove empty lines

Posted: Sat Oct 30, 2010 10:14 am
by Mistrel
I am a supporter of generous white space usage, even more so than in the past, as can be seen by my contribution history on the Tips 'n' Tricks board:

http://www.purebasic.fr/english/viewtop ... 12&t=43994

It's all part of the "Mistrel Manual of Style". :D

Re: Feature Request: Remove empty lines

Posted: Sat Oct 30, 2010 10:00 pm
by Michael Vogel
Now procedures are detected as promised...

Code: Select all

;{ AutoCompactor by Michael Vogel Vo.o2}

    ; INSTALLATION:
    ; copy compiled exe into the directory "...\Purbasic\Catalogs"
    ; install the tool using the parameter "%TEMPFILE" %SELECTION
    ; use the PureBasic as the working directory (or adapt the OpenPreferences path below)
    ; Select 'Wait until tool quits' And 'Reload source after tool has quit'

    ;    %COMPILEFILE\..\Catalogs\Tool AutoIndent.exe
    ;    "%TEMPFILE" %SELECTION
    ;    %COMPILEFILE\..
    ;    Auto &Indent
    ;    Menu Or Shortcut

    ;     × Wait until tool quits
    ;     × Reload Source after tool has quit
    ;     × into the current source


    ; DESCRIPTION:
    ; With pressed shift key:        remove ALL empty lines
    ; without shift key:            just remove lines between endprocedure/procedure
    ; no selection:                    complete source code is affected
    ; selection of lines:            only selected lines are changed


    ; HISTORY & ISSUES:
    ; n/a

;}
; Define

    #SpecialChar=Chr(1)
    
; EndDefine

Procedure Init()

    ; Dateinamen holen...
    CompilerIf 1
        If CountProgramParameters()<>2
            MessageBox_(0,"'AutoCompactor' benötigt Parameter!"+#CR$+"(%TEMPFILE und %SELECTION)","Fehler",#MB_ICONERROR| #MB_OK)
            End
        EndIf
        Global OutputFile.s=ProgramParameter()
    CompilerElse
        Global OutputFile.s="Test.pb"
    CompilerEndIf

    Global InputFile.s=OutputFile+"~"
    DeleteFile(InputFile)
    If RenameFile(OutputFile,InputFile)=0
        MessageBox_(0,"'AutoCompactor' konnte keine Temporärdatei erstellen","Fehler",#MB_ICONERROR| #MB_OK)
        End
    EndIf

    ; Selektion abfragen...
    Global zeile.l=0
    selektion.s=ProgramParameter()
    Global Startzeile.l=Val(StringField(selektion, 1, "x"))
    Global Endzeile.l=Val(StringField(selektion, 3, "x"))

    If Startzeile>=Endzeile
        Startzeile=1
        Endzeile=#MAXSHORT
    EndIf
    
EndProcedure
Procedure.s RightTrim(s.s)
    l.l=Len(s)
    While l
        l-1
        b.l=PeekB(@s+l)
        If b<>9 And b<>32
            Break
        EndIf
    Wend
    ProcedureReturn Left(s,l+1)
EndProcedure
Procedure.s LeftTrim(s.s)
    l.l=Len(s)
    c.l=0
    While c<l
        b.l=PeekB(@s+c)
        If b<>9 And b<>32 ; Space & Tab
            Break
        EndIf
        c+1
    Wend
    ProcedureReturn Mid(s,c+1,l-c)
EndProcedure

Procedure Doit(mode.l)

    Init()

    ReadFile(1,InputFile)
    CreateFile(2,OutputFile)

    Protected RemoveFlag=#True
    Protected Content.s

    While Not(Eof(1))
        z.s=ReadString(1)
        zeile.l+1

        z=righttrim(z)        ; rechts kann man immer säubern

        If zeile>=Startzeile And zeile<=Endzeile

            Content=LCase(LeftTrim(z))
            ReplaceString(Content," ",#SpecialChar,#PB_String_InPlace)
            ReplaceString(Content,";",#SpecialChar,#PB_String_InPlace)
            ReplaceString(Content,":",#SpecialChar,#PB_String_InPlace)
            ReplaceString(Content,#TAB$,#SpecialChar,#PB_String_InPlace)
            Content+#SpecialChar
    
            If mode
                RemoveFlag=#True
    
            ElseIf Left(Content,9)="procedure"
                If FindString("."+#SpecialChar,Mid(Content,10,1),1)
                    RemoveFlag=#False
                EndIf
    
            ElseIf (Left(Content,6)="macro"+#SpecialChar) Or (Left(Content,3)=#SpecialChar+"{"+#SpecialChar) Or (Left(Content,9)=#SpecialChar+#SpecialChar+"define"+#SpecialChar)
                RemoveFlag=#False
    
            ElseIf (Left(Content,13)="endprocedure"+#SpecialChar) Or (Left(Content,9)="endmacro"+#SpecialChar) Or (Left(Content,3)=#SpecialChar+"}"+#SpecialChar) Or (Left(Content,12)=#SpecialChar+#SpecialChar+"enddefine"+#SpecialChar)
                RemoveFlag=#True
    
            ;Else
            ;    MessageRequester(content,z)
            EndIf

            If (RemoveFlag=#False) Or Len(Content)>1
                WriteStringN(2,z)
            EndIf
    
        Else
    
            WriteStringN(2,z)
    
        EndIf ;

    Wend
    CloseFile(1)
    CloseFile(2)

EndProcedure
Doit((GetKeyState_(#VK_SHIFT)&$80))

Re: Feature Request: Remove empty lines

Posted: Sun Oct 31, 2010 12:55 pm
by DarkDragon
Rook Zimbabwe wrote:and REM lines... if possible... that would be good...
Rightclick: Add block comment and remove block comment? [Ctrl + B] and [Alt + B]

Re: Feature Request: Remove empty lines

Posted: Mon Nov 01, 2010 12:32 am
by WilliamL
Couldn't you just load the file into a string array and then re-save it without the empty lines (or Rems)? That would make it cross-platform. I suppose you could have a little list of options to process the code before re-saving (like empty lines/rems/only delete empty lines between procedures/etc).

Sorry, this isn't a feature request... just a thought.