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 leadingTabsToSpaces(var value: string; width: integer);
procedure leadingSpacesToTabs(var value: string; width: integer);
var
// supplementatl directories to find background tools
additionalPath: string;
@ -1347,6 +1351,58 @@ begin
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
registerClasses([TCEPersistentShortcut]);
end.

View File

@ -10,7 +10,7 @@ uses
SynHighlighterLFM, SynEditHighlighter, SynEditMouseCmds, SynEditFoldedView,
SynEditMarks, SynEditTypes, SynHighlighterJScript, SynBeautifier, dialogs,
//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_sharedres, ce_dlang, ce_stringrange, ce_dbgitf, ce_observer;
@ -210,6 +210,7 @@ type
procedure sortSelectedLines(descending, caseSensitive: boolean);
procedure tokFoundForCaption(const token: PLexToken; out stop: boolean);
procedure setGutterIcon(line: integer; value: TGutterIcon);
procedure patchClipboardIndentation;
//
procedure gutterClick(Sender: TObject; X, Y, Line: integer; mark: TSynEditMark);
procedure addBreakPoint(line: integer);
@ -1031,6 +1032,7 @@ procedure TCESynMemo.DoOnProcessCommand(var Command: TSynEditorCommand;
begin
inherited;
case Command of
ecPaste: patchClipboardIndentation;
ecCompletionMenu:
begin
fCanAutoDot:=false;
@ -2032,12 +2034,12 @@ begin
if Beautifier.isNotNil and (Beautifier is TSynBeautifier) then
begin
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
TSynBEautifier(Beautifier).IndentType := sbitConvertToTabOnly
TSynBeautifier(Beautifier).IndentType := sbitConvertToTabOnly
else
TSynBEautifier(Beautifier).IndentType := sbitSpace;
end
TSynBeautifier(Beautifier).IndentType := sbitSpace;
end;
end;
end;
@ -2331,6 +2333,39 @@ begin
result += Lines[i].length + len;
result += fMousePos.x;
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 --------------------------------------------------------------------}
{$REGION user input ------------------------------------------------------------}