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

View File

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