fix #62 - lexicon-based brace auto closing is still a bit dumb

- not trigered in comments anymore
- not trigered if the pair count is 0 (e.g between two functions)

still cases exist but they would need a more complex analysis
This commit is contained in:
Basile Burg 2016-06-15 12:35:56 +02:00
parent fb51972a58
commit 79b7b851ee
2 changed files with 85 additions and 56 deletions

View File

@ -23,6 +23,7 @@ type
fPreviousLineColum: Integer;
fBegColumnIndex: Integer;
fBegLineIndex: Integer;
fBegOffset: Integer;
function getColAndLine: TPoint;
public
constructor Create(const aText: PChar; const aColAndLine: TPoint);
@ -38,6 +39,7 @@ type
property LineAnColumn: TPoint read getColAndLine;
property SavedLine: Integer read fBegLineIndex;
property SavedColumn: Integer read fBegColumnIndex;
property SavedOffset: Integer read fBegOffset;
//
property head: PChar read fReaderHead;
end;
@ -65,6 +67,7 @@ type
PLexToken = ^TLexToken;
TLexToken = record
offset: integer;
position: TPoint;
kind: TLexTokenKind;
Data: string;
@ -206,6 +209,7 @@ procedure TReaderHead.saveBeginning;
begin
fBegColumnIndex:= fColumnIndex;
fBegLineIndex:= fLineIndex;
fBegOffset:= fAbsoluteIndex;
end;
{$ENDREGION}
@ -240,8 +244,9 @@ begin
for i:= 0 to self.count-1 do
begin
tok := getToken(i);
add(format('line %.5d - col %.3d: (%s): %s', [tok^.position.Y, tok^.position.X,
LexTokenKindString[tok^.kind], tok^.Data]));
add(format('line %.5d - col %.3d (%.8d): (%s): %s',
[tok^.position.Y, tok^.position.X, tok^.offset,
LexTokenKindString[tok^.kind], tok^.Data]));
end;
finally
SaveToFile(fname);
@ -288,6 +293,7 @@ var
ptk^.kind := aTk;
ptk^.position.X := reader.SavedColumn;
ptk^.position.Y := reader.SavedLine;
ptk^.offset := reader.savedOffset;
ptk^.Data := identifier;
list.Add(ptk);
end;

View File

@ -8,7 +8,7 @@ uses
Classes, SysUtils, controls,lcltype, Forms, graphics, ExtCtrls, crc,
SynEdit, SynPluginSyncroEdit, SynCompletion, SynEditKeyCmds, LazSynEditText,
SynHighlighterLFM, SynEditHighlighter, SynEditMouseCmds, SynEditFoldedView,
SynEditMarks, SynEditTypes, SynHighlighterJScript, dialogs,
SynEditMarks, SynEditTypes, SynHighlighterJScript, dialogs, Clipbrd,
ce_common, ce_observer, ce_writableComponent, ce_d2syn, ce_txtsyn, ce_dialogs,
ce_sharedres, ce_dlang, ce_stringrange;
@ -210,6 +210,7 @@ type
procedure showDDocs;
procedure hideDDocs;
procedure ShowPhobosDoc;
procedure copy;
//
function breakPointsCount: integer;
function breakPointLine(index: integer): integer;
@ -795,6 +796,59 @@ begin
end;
end;
procedure TCESynMemo.DoOnProcessCommand(var Command: TSynEditorCommand;
var AChar: TUTF8Char; Data: pointer);
begin
inherited;
case Command of
ecCompletionMenu:
begin
fCanAutoDot:=false;
if not fIsDSource and not alwaysAdvancedFeatures then
exit;
fCompletion.Execute(GetWordAtRowCol(LogicalCaretXY),
ClientToScreen(point(CaretXPix, CaretYPix + LineHeight)));
end;
ecPreviousLocation:
fPositions.back;
ecNextLocation:
fPositions.next;
ecShowDdoc:
begin
hideCallTips;
hideDDocs;
if not fIsDSource and not alwaysAdvancedFeatures then
exit;
showDDocs;
end;
ecShowCallTips:
begin
hideCallTips;
hideDDocs;
if not fIsDSource and not alwaysAdvancedFeatures then
exit;
showCallTips(true);
end;
ecCurlyBraceClose:
curlyBraceCloseAndIndent;
ecCommentSelection:
commentSelection;
ecSwapVersionAllNone:
invertVersionAllNone;
ecRenameIdentifier:
renameIdentifier;
ecCommentIdentifier:
commentIdentifier;
ecShowPhobosDoc:
ShowPhobosDoc;
end;
if fOverrideColMode and not SelAvail then
begin
fOverrideColMode := false;
Options := Options - [eoScrollPastEol];
end;
end;
procedure TCESynMemo.curlyBraceCloseAndIndent;
var
i: integer;
@ -976,59 +1030,6 @@ begin
end;
end;
procedure TCESynMemo.DoOnProcessCommand(var Command: TSynEditorCommand;
var AChar: TUTF8Char; Data: pointer);
begin
inherited;
case Command of
ecCompletionMenu:
begin
fCanAutoDot:=false;
if not fIsDSource and not alwaysAdvancedFeatures then
exit;
fCompletion.Execute(GetWordAtRowCol(LogicalCaretXY),
ClientToScreen(point(CaretXPix, CaretYPix + LineHeight)));
end;
ecPreviousLocation:
fPositions.back;
ecNextLocation:
fPositions.next;
ecShowDdoc:
begin
hideCallTips;
hideDDocs;
if not fIsDSource and not alwaysAdvancedFeatures then
exit;
showDDocs;
end;
ecShowCallTips:
begin
hideCallTips;
hideDDocs;
if not fIsDSource and not alwaysAdvancedFeatures then
exit;
showCallTips(true);
end;
ecCurlyBraceClose:
curlyBraceCloseAndIndent;
ecCommentSelection:
commentSelection;
ecSwapVersionAllNone:
invertVersionAllNone;
ecRenameIdentifier:
renameIdentifier;
ecCommentIdentifier:
commentIdentifier;
ecShowPhobosDoc:
ShowPhobosDoc;
end;
if fOverrideColMode and not SelAvail then
begin
fOverrideColMode := false;
Options := Options - [eoScrollPastEol];
end;
end;
procedure TCESynMemo.invertVersionAllNone;
var
i: integer;
@ -1212,6 +1213,14 @@ begin
shellOpen(pth);
end;
procedure TCESynMemo.copy;
begin
{$IFDEF WINDOWS}
{$ELSE}
// workaround https://github.com/BBasile/Coedit/issues/39
{$ENDIF}
end;
{$ENDREGION}
{$REGION DDoc & CallTip --------------------------------------------------------}
@ -1422,14 +1431,28 @@ end;
function TCESynMemo.lexCanCloseBrace: boolean;
var
i: integer;
p: integer;
c: integer = 0;
tok: PLexToken;
ton: PLexToken;
bet: boolean;
begin
p := SelStart;
for i := 0 to fLexToks.Count-1 do
begin
tok := fLexToks[i];
if (i <> fLexToks.Count-1) then
begin
ton := fLexToks[i+1];
bet := (tok^.offset + 1 <= p) and (ton^.offset + 1 > p);
end else
bet := false;
if bet and (tok^.kind = ltkComment) then
exit(false);
c += byte((tok^.kind = TLexTokenKind.ltkSymbol) and (((tok^.Data = '{')) or (tok^.Data = 'q{')));
c -= byte((tok^.kind = TLexTokenKind.ltkSymbol) and (tok^.Data = '}'));
if bet and (c = 0) then
exit(false);
end;
if (tok <> nil) and (tok^.kind = ltkIllegal) then
result := false