Skip to content

Commit

Permalink
Fix typing assist #87
Browse files Browse the repository at this point in the history
- wrong caret position when hitting enter
- table content is not aligned with the header
  • Loading branch information
alexandrafung committed Jun 10, 2021
1 parent 74a6f2e commit 89d288b
Showing 1 changed file with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,13 @@ private bool HandleEnter([NotNull] IActionContext context)

if (inTable)
{
if (GetFormatSettingsKey(textControl).SmallTableIndent)
extraIdent = " ";
else
extraIdentSize += GetFormatSettingsKey(textControl).TableIndentSize;
var indentSizeBasedOnPreviousRow = FindPreviousRowFirstPipeIndent(cachingLexer, caret);
var indentText = GetTableIndentText(indentSizeBasedOnPreviousRow);
textControl.Document.InsertText(caret, GetNewLineText(textControl) + indentText);
return true;
}

var currentIndent = ComputeIndentOfCurrentKeyword(cachingLexer) + GetIndentText(textControl, extraIdentSize) + extraIdent;

textControl.Document.InsertText(caret, GetNewLineText(textControl) + currentIndent);
return true;
}
Expand All @@ -114,6 +113,16 @@ private bool HandleEnter([NotNull] IActionContext context)
return false;
}

private static string GetTableIndentText(int indent)
{
var sb = new StringBuilder();
for (var i = 0; i < indent; i++)
{
sb.Append(" ");
}
return sb.ToString();
}

private bool HandleTableCellClosing([NotNull] ITypingContext context)
{
var textControl = context.TextControl;
Expand All @@ -128,7 +137,7 @@ private bool HandleTableCellClosing([NotNull] ITypingContext context)
PsiServices.Transactions.Execute("Format code", () =>
{
GetCodeFormatter(tokenNode).Format(parentTable.firstChild, parentTable.lastChild.LastChild, CodeFormatProfile.SOFT, new AdditionalFormatterParameters(treatTextAfterLastNodeAsIncorrect: false));
GetCodeFormatter(tokenNode).Format(parentTable.firstChild.FirstChild, parentTable.lastChild.LastChild, CodeFormatProfile.SOFT, new AdditionalFormatterParameters(treatTextAfterLastNodeAsIncorrect: false));
});
return null;
}));
Expand Down Expand Up @@ -188,6 +197,32 @@ private TokenNodeType FindLastKeywordToken(CachingLexer cachingLexer, int caret,
}
return cachingLexer.TokenType;
}

private int FindPreviousRowFirstPipeIndent(CachingLexer cachingLexer, int caret)
{
//find previous enter
cachingLexer.FindTokenAt(caret - 1);
while (GherkinTokenTypes.NEW_LINE != cachingLexer.TokenType)
{
if (cachingLexer.CurrentPosition == 0)
return 0;
cachingLexer.SetCurrentToken(cachingLexer.CurrentPosition - 1);
}

var enterStart = cachingLexer.TokenStart;

//find the first pipe after the enter
while (GherkinTokenTypes.PIPE != cachingLexer.TokenType)
{
if (cachingLexer.CurrentPosition == 0)
return 0;
cachingLexer.SetCurrentToken(cachingLexer.CurrentPosition + 1);
}

var pipeStart = cachingLexer.TokenStart;

return pipeStart - enterStart -1;
}


private IndentStyle GetIndentType(ITextControl textControl)
Expand Down

0 comments on commit 89d288b

Please sign in to comment.