Skip to content

Commit

Permalink
Disable code completion in case of adding spaces before a step. Fix #147
Browse files Browse the repository at this point in the history
  • Loading branch information
Socolin committed Jul 17, 2022
1 parent caf3bae commit c0a8108
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using JetBrains.DocumentModel;
using JetBrains.ReSharper.Feature.Services.CodeCompletion.Impl;
using JetBrains.ReSharper.Feature.Services.CodeCompletion.Infrastructure;
Expand All @@ -20,6 +23,8 @@ public override ISpecificCodeCompletionContext GetCompletionContext(CodeCompleti
var relatedText = string.Empty;
var nodeUnderCursor = TextControlToPsi.GetElement<ITreeNode>(context.Solution, context.TextControl);

if (IsInIndentPart(nodeUnderCursor))
return null;
if (IsAfterKeywordExpectingText(nodeUnderCursor))
return null;

Expand Down Expand Up @@ -115,8 +120,37 @@ private bool IsAtEmptyLineBeforeEndOfFile(ITreeNode node)
return true;
}


private bool IsInIndentPart(ITreeNode nodeUnderCursor)
{
if (!IsAtLineStart(nodeUnderCursor))
return false;

return GetNodesUntilEol(nodeUnderCursor.NextSibling).Any(x => !x.IsWhitespaceToken());
}

private bool IsAtLineStart(ITreeNode nodeUnderCursor)
{
if (nodeUnderCursor.NodeType == GherkinTokenTypes.NEW_LINE)
return true;

return nodeUnderCursor.IsWhitespaceToken() && nodeUnderCursor.PrevSibling?.NodeType == GherkinTokenTypes.NEW_LINE;
}

[ItemCanBeNull]
private IEnumerable<ITreeNode> GetNodesUntilEol(ITreeNode node)
{
while (node != null && node.NodeType != GherkinTokenTypes.NEW_LINE)
{
yield return node;
node = node.NextSibling;
}
}

private bool IsAfterKeywordExpectingText(ITreeNode nodeUnderCursor)
{
if (nodeUnderCursor.NodeType == GherkinTokenTypes.NEW_LINE)
return false;
if (nodeUnderCursor.IsWhitespaceToken()
|| nodeUnderCursor is GherkinToken token && (token.NodeType == GherkinTokenTypes.TEXT || token.NodeType == GherkinTokenTypes.COLON))
{
Expand Down

0 comments on commit c0a8108

Please sign in to comment.