Skip to content

Fix bugs and docs of FilePositionMap #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 19, 2018
Merged
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
19 changes: 12 additions & 7 deletions src/FilePositionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FilePositionMap {
/** @var int the 0-based byte offset of the most recent request for a line number. */
private $currentOffset;

/** @var int the line number for $this->currentOffset (updated whenever currentOffset is updated) */
/** @var int the 1-based line number for $this->currentOffset (updated whenever currentOffset is updated) */
private $lineForCurrentOffset;

public function __construct(string $file_contents) {
Expand All @@ -45,7 +45,7 @@ public function getNodeStartLine(Node $node) : int {
}

/**
* @param Node $node the node to get the start line for.
* @param Token $token the token to get the start line for.
*/
public function getTokenStartLine(Token $token) : int {
return $this->getLineNumberForOffset($token->start);
Expand Down Expand Up @@ -85,20 +85,24 @@ public function getEndLine($node) : int {
* @param Node|Token $node
* Similar to getStartLine but includes the column
*/
public function getEndLineCharacterPositionForOffset($node) : LineCharacterPosition {
return $this->getLineCharacterPositionForOffset($node);
public function getEndLineCharacterPosition($node) : LineCharacterPosition {
return $this->getLineCharacterPositionForOffset($node->getEndPosition());
}

/**
* @param Node|Token $node
* Similar to getStartLine but includes the column
* @param int $offset
* Similar to getStartLine but includes both the line and the column
*/
public function getLineCharacterPositionForOffset(int $offset) : LineCharacterPosition {
$line = $this->getLineNumberForOffset($offset);
$character = $this->getColumnForOffset($offset);
return new LineCharacterPosition($line, $character);
}

/**
* @param int $offset - A 0-based byte offset
* @return int - gets the 1-based line number for $offset
*/
public function getLineNumberForOffset(int $offset) : int {
if ($offset < 0) {
$offset = 0;
Expand All @@ -117,7 +121,8 @@ public function getLineNumberForOffset(int $offset) : int {
}

/**
* @return int - gets the 1-based column offset
* @param int $offset - A 0-based byte offset
* @return int - gets the 1-based column number for $offset
*/
public function getColumnForOffset(int $offset) : int {
$length = $this->fileContentsLength;
Expand Down