Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Sccarda/loop range simplify code action #84

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/QsCompiler/CompilationManager/DataStructures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,30 @@ internal TokenIndex(FileContentManager file, int line, int index)
this.Index = index;
}

internal TokenIndex(FileContentManager file, Position position)
{
this.File = file ?? throw new ArgumentNullException(nameof(file));
if (position.Line < 0 || position.Line >= file.NrTokenizedLines()) throw new ArgumentOutOfRangeException(nameof(position));

this.Line = position.Line;

int index = -1;
var line = file.GetTokenizedLine(position.Line);
for (int i = 0; i < line.Count(); i++)
{
CodeFragment frag = line[i];
// if the given position is within the fragment
if (frag.GetRange().Start.Character <= position.Character &&
frag.GetRange().End.Character >= position.Character)
{
index = i;
break;
}
}
if (index < 0) throw new ArgumentOutOfRangeException(nameof(position));
this.Index = index;
}

internal TokenIndex(TokenIndex tIndex)
: this(tIndex.File, tIndex.Line, tIndex.Index) { }

Expand Down
Loading