fix #106, "Paste" should patch the leading blanks according to the indent mode

spaces 2 tabs is not handled because of a limitation in TSynEdit
This commit is contained in:
Basile Burg 2016-11-22 05:00:31 +01:00
parent 7a673e96e9
commit 459f5add40
No known key found for this signature in database
GPG Key ID: 1868039F415CB8CF
2 changed files with 96 additions and 5 deletions

View File

@ -299,6 +299,10 @@ type
procedure tryRaiseFromStdErr(proc: TProcess); procedure tryRaiseFromStdErr(proc: TProcess);
procedure leadingTabsToSpaces(var value: string; width: integer);
procedure leadingSpacesToTabs(var value: string; width: integer);
var var
// supplementatl directories to find background tools // supplementatl directories to find background tools
additionalPath: string; additionalPath: string;
@ -1347,6 +1351,58 @@ begin
end; end;
end; end;
procedure leadingTabsToSpaces(var value: string; width: integer);
var
m: integer;
s: string;
begin
if value.length = 0 then
exit;
m := 1;
while true do
begin
if value[m] <> #9 then
break;
if m = value.length then
break;
m += 1;
end;
width *= (m - 1);
setLength(s, width);
if s.length <> 0 then
fillChar(s[1], width, ' ');
value := s + value[m..value.length];
end;
procedure leadingSpacesToTabs(var value: string; width: integer);
var
m: integer;
t: string;
begin
if value.length = 0 then
exit;
m := 1;
while true do
begin
if value[m] <> ' ' then
break;
if m = value.length then
break;
m += 1;
end;
width := (m - 1) div width;
setLength(t, width);
if t.length <> 0 then
fillChar(t[1], width, #9);
value := t + value[m..value.length];
end;
initialization initialization
registerClasses([TCEPersistentShortcut]); registerClasses([TCEPersistentShortcut]);
end. end.

View File

@ -10,7 +10,7 @@ uses
SynHighlighterLFM, SynEditHighlighter, SynEditMouseCmds, SynEditFoldedView, SynHighlighterLFM, SynEditHighlighter, SynEditMouseCmds, SynEditFoldedView,
SynEditMarks, SynEditTypes, SynHighlighterJScript, SynBeautifier, dialogs, SynEditMarks, SynEditTypes, SynHighlighterJScript, SynBeautifier, dialogs,
//SynEditMarkupFoldColoring, //SynEditMarkupFoldColoring,
fpjson, jsonparser, LazUTF8, LazUTF8Classes, Buttons, StdCtrls, Clipbrd, fpjson, jsonparser, LazUTF8, LazUTF8Classes, Buttons, StdCtrls,
ce_common, ce_writableComponent, ce_d2syn, ce_txtsyn, ce_dialogs, ce_common, ce_writableComponent, ce_d2syn, ce_txtsyn, ce_dialogs,
ce_sharedres, ce_dlang, ce_stringrange, ce_dbgitf, ce_observer; ce_sharedres, ce_dlang, ce_stringrange, ce_dbgitf, ce_observer;
@ -210,6 +210,7 @@ type
procedure sortSelectedLines(descending, caseSensitive: boolean); procedure sortSelectedLines(descending, caseSensitive: boolean);
procedure tokFoundForCaption(const token: PLexToken; out stop: boolean); procedure tokFoundForCaption(const token: PLexToken; out stop: boolean);
procedure setGutterIcon(line: integer; value: TGutterIcon); procedure setGutterIcon(line: integer; value: TGutterIcon);
procedure patchClipboardIndentation;
// //
procedure gutterClick(Sender: TObject; X, Y, Line: integer; mark: TSynEditMark); procedure gutterClick(Sender: TObject; X, Y, Line: integer; mark: TSynEditMark);
procedure addBreakPoint(line: integer); procedure addBreakPoint(line: integer);
@ -1031,6 +1032,7 @@ procedure TCESynMemo.DoOnProcessCommand(var Command: TSynEditorCommand;
begin begin
inherited; inherited;
case Command of case Command of
ecPaste: patchClipboardIndentation;
ecCompletionMenu: ecCompletionMenu:
begin begin
fCanAutoDot:=false; fCanAutoDot:=false;
@ -2032,12 +2034,12 @@ begin
if Beautifier.isNotNil and (Beautifier is TSynBeautifier) then if Beautifier.isNotNil and (Beautifier is TSynBeautifier) then
begin begin
if not (eoTabsToSpaces in Options) and not (eoSpacesToTabs in Options) then if not (eoTabsToSpaces in Options) and not (eoSpacesToTabs in Options) then
TSynBEautifier(Beautifier).IndentType := sbitConvertToTabOnly TSynBeautifier(Beautifier).IndentType := sbitConvertToTabOnly
else if eoSpacesToTabs in options then else if eoSpacesToTabs in options then
TSynBEautifier(Beautifier).IndentType := sbitConvertToTabOnly TSynBeautifier(Beautifier).IndentType := sbitConvertToTabOnly
else else
TSynBEautifier(Beautifier).IndentType := sbitSpace; TSynBeautifier(Beautifier).IndentType := sbitSpace;
end end;
end; end;
end; end;
@ -2331,6 +2333,39 @@ begin
result += Lines[i].length + len; result += Lines[i].length + len;
result += fMousePos.x; result += fMousePos.x;
end; end;
procedure TCESynMemo.patchClipboardIndentation;
var
lst: TStringList;
lne: string;
i: integer;
begin
//TODO-cCheck for changes made to option eoSpacesToTabs
if not (eoTabsToSpaces in Options) then
exit;
lst := TStringList.Create;
lst.Text:=clipboard.asText;
try
for i := 0 to lst.count-1 do
begin
lne := lst[i];
//if eoTabsToSpaces in Options then
//begin
leadingTabsToSpaces(lne, TabWidth);
lst[i] := lne;
//end
{else if eoSpacesToTabs in Options then
begin
//leadingSpacesToTabs(lne, TabWidth);
//lst[i] := lne;
end}
end;
clipboard.asText := lst.Text;
finally
lst.free;
end;
end;
{$ENDREGION --------------------------------------------------------------------} {$ENDREGION --------------------------------------------------------------------}
{$REGION user input ------------------------------------------------------------} {$REGION user input ------------------------------------------------------------}