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 1 commit
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
17 changes: 11 additions & 6 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 @@ -86,19 +86,23 @@ public function getEndLine($node) : int {
* Similar to getStartLine but includes the column
*/
public function getEndLineCharacterPositionForOffset($node) : LineCharacterPosition {
return $this->getLineCharacterPositionForOffset($node);
return $this->getLineCharacterPositionForOffset($node->getEndPosition());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other ForOffset methods take an actual offset, should this one have a different name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can $node have a type annotation?

Copy link
Contributor Author

@TysonAndre TysonAndre Feb 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

     /**
      * @param Node|Token $node
      * Similar to getStartLine but includes the column
      */

It has a phpdoc type annotation. It's used both for Node and Token, which have no common interface.

Renamed this method, the original name was incorrect

}

/**
* @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