JediCodeFormat: Use Ctrl-D shortcut for selection and whole unit. Copy right amount of whitespace into selection. Cleanup. Issue #37652, patch from Domingo Galmés.

git-svn-id: trunk@63853 -
This commit is contained in:
juha 2020-08-31 19:35:01 +00:00
parent e0b2abcbe7
commit ef45d65900
2 changed files with 39 additions and 53 deletions

View File

@ -127,11 +127,15 @@ begin
if (SourceEditorManagerIntf= nil) or (SourceEditorManagerIntf.ActiveEditor = nil) then if (SourceEditorManagerIntf= nil) or (SourceEditorManagerIntf.ActiveEditor = nil) then
LogIdeMessage('', 'No current window', mtInputError, -1, -1) LogIdeMessage('', 'No current window', mtInputError, -1, -1)
else begin else begin
lsMsg := Format(lisJEDICodeFormatOfStartFormatting, [SourceEditorManagerIntf if SourceEditorManagerIntf.ActiveEditor.SelectionAvailable then
.ActiveEditor.FileName DoFormatSelection(Sender)
+ NativeLineBreak]); else
if CanFormat(lsMsg) then begin
ConvertEditor(SourceEditorManagerIntf.ActiveEditor) lsMsg := Format(lisJEDICodeFormatOfStartFormatting,
[SourceEditorManagerIntf.ActiveEditor.FileName + NativeLineBreak]);
if CanFormat(lsMsg) then
ConvertEditor(SourceEditorManagerIntf.ActiveEditor)
end;
end; end;
end; end;
@ -156,9 +160,8 @@ begin
lazProject := GetCurrentProject; lazProject := GetCurrentProject;
if lazProject = nil then if lazProject = nil then
exit; exit;
lsMsg := Format(lisJEDICodeFormatOfAreYouSureThatYouWantToFormatAllFi, [ lsMsg := Format(lisJEDICodeFormatOfAreYouSureThatYouWantToFormatAllFi,
lazProject.MainFile.FileName + NativeLineBreak, IntToStr(lazProject. [lazProject.MainFile.FileName + NativeLineBreak, IntToStr(lazProject.FileCount)]);
FileCount)]);
if CanFormat(lsMsg) then if CanFormat(lsMsg) then
begin begin
ClearToolMessages; ClearToolMessages;
@ -280,18 +283,13 @@ var
begin begin
p1 := srcEditor.BlockBegin; p1 := srcEditor.BlockBegin;
p2 := srcEditor.BlockEnd; p2 := srcEditor.BlockEnd;
if (p1.y > p2.y) then if p1.y > p2.y then
begin begin
p1 := srcEditor.BlockEnd; p1 := srcEditor.BlockEnd;
p2 := srcEditor.BlockBegin; p2 := srcEditor.BlockBegin;
end; end;
if p2.x<=1 then if (p2.x <= 1) and (p2.y > 1) then
begin p2.y := p2.y-1;
if p2.y>1 then
p2.y:=p2.y-1;
end;
p2.x:=Length(srcEditor.Lines[p2.y-1])+1; //last char
p1.x:=1;
end; end;
var var
@ -308,7 +306,7 @@ begin
exit; exit;
end; end;
srcEditor := SourceEditorManagerIntf.ActiveEditor; srcEditor := SourceEditorManagerIntf.ActiveEditor;
if (srcEditor.SelectionAvailable=false) or srcEditor.ReadOnly then if not srcEditor.SelectionAvailable or srcEditor.ReadOnly then
Exit; Exit;
sourceCode := srcEditor.GetText(False); //get ALL editor text. sourceCode := srcEditor.GetText(False); //get ALL editor text.
GetSelectedBlockFullLines(BlockBegin,BlockEnd); GetSelectedBlockFullLines(BlockBegin,BlockEnd);
@ -318,12 +316,15 @@ begin
fcConverter.InputCode := sourceCode; fcConverter.InputCode := sourceCode;
fcConverter.GuiMessages := true; fcConverter.GuiMessages := true;
FindLineOffsets(sourceCode,BlockBegin.Y,BlockEnd.Y,lineStartOffset,lineEndOffset); FindLineOffsets(sourceCode,BlockBegin.Y,BlockEnd.Y,lineStartOffset,lineEndOffset);
fcConverter.ConvertPart(lineStartOffset,lineEndOffset,true); fcConverter.ConvertPart(lineStartOffset, lineEndOffset, True);
wI := length(fcConverter.OutputCode);
// converter adds 2 LineEndings.
outputstr := Copy(fcConverter.OutputCode, 1, wI-Length(LineEnding+LineEnding));
if not fcConverter.ConvertError then if not fcConverter.ConvertError then
srcEditor.ReplaceText(BlockBegin, BlockEnd, outputstr); begin
wI := length(fcConverter.OutputCode);
while (wI > 1) and (fcConverter.OutputCode[wI] in [#10, #13, ' ']) do
Dec(wI);
outputstr := Copy(fcConverter.OutputCode, 1, wI);
srcEditor.ReplaceLines(BlockBegin.Y, BlockEnd.Y, outputstr, false);
end;
finally finally
fcConverter.Free; fcConverter.Free;
end; end;
@ -392,7 +393,6 @@ begin
fcEditorConverter := TEditorConverter.Create; fcEditorConverter := TEditorConverter.Create;
fcEditorConverter.OnStatusMessage := LogIDEMessage; fcEditorConverter.OnStatusMessage := LogIDEMessage;
end; end;
Assert(fcEditorConverter <> nil); Assert(fcEditorConverter <> nil);
end; end;
@ -403,7 +403,6 @@ begin
lazMessages := IDEMessagesWindow; lazMessages := IDEMessagesWindow;
if lazMessages = nil then if lazMessages = nil then
exit; exit;
lazMessages.Clear; lazMessages.Clear;
end; end;

View File

@ -33,10 +33,15 @@ See http://www.gnu.org/licenses/gpl.html
interface interface
uses uses
{ rtl }
SysUtils, Classes, SysUtils, Classes,
{ lcl } // LCL
LCLType, LazFileUtils, LazUTF8; LCLType,
// LazUtils
LazFileUtils,
// IdeIntf
LazIDEIntf, MenuIntf, IdeCommands,
// local
JcfIdeMain, JcfRegistrySettings;
procedure Register; procedure Register;
@ -44,19 +49,11 @@ implementation
{$R jcfsettings.res} {$R jcfsettings.res}
uses
{ lazarus }
LazIDEIntf, MenuIntf, IdeCommands,
{ local }
JcfIdeMain, JcfRegistrySettings;
const const
FORMAT_MENU_NAME = 'jcfJEDICodeFormat'; FORMAT_MENU_NAME = 'jcfJEDICodeFormat';
FORMAT_SELECTION_NAME = 'jcfSelectionText';
FORMAT_CURRENT_NAME = 'jcfCurrentEditorWindow'; FORMAT_CURRENT_NAME = 'jcfCurrentEditorWindow';
FORMAT_PROJECT_MENU_NAME = 'jcfAllFilesinProject'; FORMAT_PROJECT_MENU_NAME = 'jcfAllFilesinProject';
FORMAT_OPEN_MENU_NAME = 'jcfAllOpenWindows'; FORMAT_OPEN_MENU_NAME = 'jcfAllOpenWindows';
//FORMAT_REG_SETTINGS_MENU_NAME = 'jcfRegistrySettings';
FORMAT_SETTINGS_MENU_NAME = 'jcfFormatSettings'; FORMAT_SETTINGS_MENU_NAME = 'jcfFormatSettings';
FORMAT_ABOUT_MENU_NAME = 'jcfAbout'; FORMAT_ABOUT_MENU_NAME = 'jcfAbout';
FORMAT_CATEGORY_IDECMD_NAME = 'jcfFormat'; FORMAT_CATEGORY_IDECMD_NAME = 'jcfFormat';
@ -65,13 +62,11 @@ const
resourcestring resourcestring
FORMAT_MENU = 'JEDI Code &Format'; FORMAT_MENU = 'JEDI Code &Format';
FORMAT_SELECTION_MENU = 'Selection';
FORMAT_CURRENT_MENU = '&Current Editor Window'; FORMAT_CURRENT_MENU = '&Current Editor Window';
FORMAT_SELECTION_IDECMD = 'Format code in Selection'; FORMAT_SELECTION_IDECMD = 'Format code in Selection';
FORMAT_CURRENT_IDECMD = 'Format code in current editor window'; FORMAT_CURRENT_IDECMD = 'Format code in current editor window';
FORMAT_PROJECT_MENU = '&All Files in Project'; FORMAT_PROJECT_MENU = '&All Files in Project';
FORMAT_OPEN_MENU = 'All &Open Windows'; FORMAT_OPEN_MENU = 'All &Open Windows';
//FORMAT_REG_SETTINGS_MENU = '&Registry Settings';
FORMAT_SETTINGS_MENU = '&Format Settings'; FORMAT_SETTINGS_MENU = '&Format Settings';
FORMAT_ABOUT_MENU = '&About'; FORMAT_ABOUT_MENU = '&About';
FORMAT_CATEGORY_IDECMD = 'JEDI Code Format'; FORMAT_CATEGORY_IDECMD = 'JEDI Code Format';
@ -95,11 +90,11 @@ var
begin begin
JCFOptsFile := AppendPathDelim(LazarusIDE.GetPrimaryConfigPath) + DefaultJCFOptsFile; JCFOptsFile := AppendPathDelim(LazarusIDE.GetPrimaryConfigPath) + DefaultJCFOptsFile;
LazarusIDE.CopySecondaryConfigFile(DefaultJCFOptsFile); LazarusIDE.CopySecondaryConfigFile(DefaultJCFOptsFile);
if not FileExistsUTF8(JCFOptsFile) then if not FileExists(JCFOptsFile) then
begin begin
// create default // create default
R := TResourceStream.Create(HInstance, PChar('JCFSettings'), PChar(RT_RCDATA)); R := TResourceStream.Create(HInstance, PChar('JCFSettings'), PChar(RT_RCDATA));
S := TFileStream.Create(UTF8ToSys(JCFOptsFile), fmCreate); S := TFileStream.Create(JCFOptsFile, fmCreate);
try try
S.CopyFrom(R, R.Size); S.CopyFrom(R, R.Size);
finally finally
@ -113,8 +108,8 @@ procedure Register;
var var
Cat: TIDECommandCategory; Cat: TIDECommandCategory;
fcMainMenu, SubSection: TIDEMenuSection; fcMainMenu, SubSection: TIDEMenuSection;
KeySelect, KeyUnit: TIDEShortCut; Key: TIDEShortCut;
CmdSelect, CmdUnit: TIDECommand; Cmd: TIDECommand;
begin begin
SetLazarusDefaultFileName; SetLazarusDefaultFileName;
GetDefaultSettingsFileName := IDEGetDefaultSettingsFileName; GetDefaultSettingsFileName := IDEGetDefaultSettingsFileName;
@ -124,20 +119,12 @@ begin
fcMainMenu := RegisterIDESubMenu(itmSourceTools, FORMAT_MENU_NAME, FORMAT_MENU); fcMainMenu := RegisterIDESubMenu(itmSourceTools, FORMAT_MENU_NAME, FORMAT_MENU);
KeySelect := IDEShortCut(VK_UNKNOWN, []);
// We are running out of free shortcut combinations. Ctrl+Shift+Alt+D is free.
//KeySelect := IDEShortCut(VK_D, [ssShift,ssAlt,SSctrl]);
CmdSelect := RegisterIDECommand(Cat, FORMAT_SELECTION_NAME, FORMAT_SELECTION_IDECMD,
KeySelect, lcJCFIDE.DoFormatSelection);
RegisterIDEMenuCommand(fcMainMenu, FORMAT_SELECTION_NAME, FORMAT_SELECTION_MENU,
lcJCFIDE.DoFormatSelection, nil, CmdSelect);
// Ctrl + D // Ctrl + D
KeyUnit := IDEShortCut(VK_D, [SSctrl]); Key := IDEShortCut(VK_D, [SSctrl]);
CmdUnit := RegisterIDECommand(Cat, FORMAT_CURRENT_NAME, FORMAT_CURRENT_IDECMD, Cmd := RegisterIDECommand(Cat, FORMAT_CURRENT_NAME, FORMAT_CURRENT_IDECMD,
KeyUnit, lcJCFIDE.DoFormatCurrentIDEWindow); Key, lcJCFIDE.DoFormatCurrentIDEWindow);
RegisterIDEMenuCommand(fcMainMenu, FORMAT_CURRENT_NAME, FORMAT_CURRENT_MENU, RegisterIDEMenuCommand(fcMainMenu, FORMAT_CURRENT_NAME, FORMAT_CURRENT_MENU,
lcJCFIDE.DoFormatCurrentIDEWindow, nil, CmdUnit); lcJCFIDE.DoFormatCurrentIDEWindow, nil, Cmd);
RegisterIDEMenuCommand(fcMainMenu, FORMAT_PROJECT_MENU_NAME, FORMAT_PROJECT_MENU, RegisterIDEMenuCommand(fcMainMenu, FORMAT_PROJECT_MENU_NAME, FORMAT_PROJECT_MENU,
lcJCFIDE.DoFormatProject); lcJCFIDE.DoFormatProject);