Page 1 of 1
PB IDE: add Edit Selection Case functionality
Posted: Thu Jun 08, 2017 3:01 pm
by Tristano
It would be really nice to have a menu in PB's IDE to change the case of selected text (lowercase / uppercase / title case), and to have a predefined keyboard shortcut to toggle through the different cases (like in MS Word, where pressing Shift + F3 cycles through all available cases).
Apart from simplifying reformatting strings, it would also be very useful for reformatting comments — often one changes his/her mind regarding text casing, or simply has typed a long text without realizing that SHIFT-LOCK was on, and text case functionality would avoid having to retype the whole text again in the desired case.
Many popular code editors have this functionality, so it would be cool to see it in PB IDE as well.
As for title-casing, usually editors either follow the English rules for titles or just capitalize every first letter — every language has it's own rules when it comes to title casing, and some would be rather difficult to implement, but the English rules are quite easy to implement. Capitalizing every first letter might be a more "universal" approach, without following any particular language.
Re: PB IDE: add Edit Selection Case functionality
Posted: Thu Jun 08, 2017 5:51 pm
by skywalk
Good idea.
Alternative is search and/or write custom TOOL code.
Re: PB IDE: add Edit Selection Case functionality
Posted: Thu Jun 08, 2017 6:25 pm
by STARGÅTE
Nice idea.
Here are the code for the IDE-Tool.
Just compile it, and add it in you tool list without any parameters!
Code: Select all
Procedure GetProcessFromWindow(WindowID.i)
Protected ProcessID.i
If GetWindowThreadProcessId_(WindowID, @ProcessID)
ProcedureReturn OpenProcess_(#PROCESS_ALL_ACCESS, #False, ProcessID)
EndIf
EndProcedure
; Gets the current selected text in the editor.
Procedure.s GetSelectionText()
Protected ScintillaID.i = Val(GetEnvironmentVariable("PB_TOOL_Scintilla"))
Protected ProcessID.i = GetProcessFromWindow(ScintillaID)
Protected Length.i
Protected *MemoryID, *Buffer, Format.i, Text.s
If ProcessID
Select SendMessage_(ScintillaID, #SCI_GETCODEPAGE, #Null, #Null)
Case 0 : Format = #PB_Ascii
Case 65001 : Format = #PB_UTF8
EndSelect
Length = SendMessage_(ScintillaID, #SCI_GETSELTEXT, #Null, #Null)
*Buffer = AllocateMemory(Length+SizeOf(Character))
If *Buffer
*MemoryID = VirtualAllocEx_(ProcessID, #Null, Length+SizeOf(Character), #MEM_RESERVE|#MEM_COMMIT, #PAGE_EXECUTE_READWRITE)
If *MemoryID
SendMessage_(ScintillaID, #SCI_GETSELTEXT, #Null, *MemoryID)
ReadProcessMemory_(ProcessID, *MemoryID, *Buffer, Length, #Null)
VirtualFreeEx_(ProcessID, *MemoryID, Length+SizeOf(Character), #MEM_RELEASE)
EndIf
Text = PeekS(*Buffer, -1, Format)
FreeMemory(*Buffer)
EndIf
CloseHandle_(ProcessID)
EndIf
ProcedureReturn Text
EndProcedure
; Replaces the current selected text in the editor with the specified text.
Procedure SetSelectionText(Text.s)
Protected ScintillaID.i = Val(GetEnvironmentVariable("PB_TOOL_Scintilla"))
Protected ProcessID.i = GetProcessFromWindow(ScintillaID)
Protected Length.i, Position.i
Protected *MemoryID, *Buffer, Format.i
If ProcessID
Select SendMessage_(ScintillaID, #SCI_GETCODEPAGE, #Null, #Null)
Case 0 : Format = #PB_Ascii
Case 65001 : Format = #PB_UTF8
EndSelect
Length.i = StringByteLength(Text, Format)
*Buffer = AllocateMemory(Length+SizeOf(Character))
If *Buffer
PokeS(*Buffer, Text, #PB_Default, Format)
*MemoryID = VirtualAllocEx_(ProcessID, #Null, Length, #MEM_RESERVE|#MEM_COMMIT, #PAGE_EXECUTE_READWRITE)
If *MemoryID
WriteProcessMemory_(ProcessID, *MemoryID, *Buffer, Length, #Null)
Position = SendMessage_(ScintillaID, #SCI_GETSELECTIONSTART, #Null, #Null)
SendMessage_(ScintillaID, #SCI_REPLACESEL, #Null, *MemoryID)
SendMessage_(ScintillaID, #SCI_SETSELECTIONSTART, Position, #Null)
SendMessage_(ScintillaID, #SCI_SETSELECTIONEND, Position+Length, #Null)
VirtualFreeEx_(ProcessID, *MemoryID, Length, #MEM_RELEASE)
EndIf
FreeMemory(*Buffer)
EndIf
CloseHandle_(ProcessID)
EndIf
EndProcedure
Procedure.s TCase(Text.s)
Protected *Character.Character = @Text
Protected Change.i = #True
If *Character
While *Character\c <> 0
If *Character\c = 32
Change = #True
ElseIf Change
*Character\c = Asc(UCase(Chr(*Character\c)))
Change = #False
EndIf
*Character + SizeOf(Character)
Wend
EndIf
ProcedureReturn Text
EndProcedure
Define Text.s = GetSelectionText()
If Text = UCase(Text)
SetSelectionText(TCase(LCase(Text)))
ElseIf Text = LCase(Text)
SetSelectionText(UCase(Text))
Else
SetSelectionText(LCase(Text))
EndIf
Example selection:
Code: Select all
; Gets the current selected text in the editor.
; gets the current selected text in the editor.
; GETS THE CURRENT SELECTED TEXT IN THE EDITOR.
; Gets The Current Selected Text In The Editor.
Re: PB IDE: add Edit Selection Case functionality
Posted: Fri Jun 09, 2017 1:39 pm
by Tristano
Thanks STARGÅTE !
This is going to be really useful.