PB IDE: add Edit Selection Case functionality

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Tristano
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Nov 26, 2015 6:52 pm
Location: Italy
Contact:

PB IDE: add Edit Selection Case functionality

Post 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.
The PureBASIC Archives: FOSS Resources:
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: PB IDE: add Edit Selection Case functionality

Post by skywalk »

Good idea.
Alternative is search and/or write custom TOOL code.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: PB IDE: add Edit Selection Case functionality

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Tristano
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Nov 26, 2015 6:52 pm
Location: Italy
Contact:

Re: PB IDE: add Edit Selection Case functionality

Post by Tristano »

Thanks STARGÅTE !

This is going to be really useful.
The PureBASIC Archives: FOSS Resources:
Post Reply