Skip to content

Commit

Permalink
UTF8-safe snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Mar 1, 2021
1 parent 1ab0a5d commit e1e0d3c
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/Psalm/CodeLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
use function preg_replace;
use Psalm\Internal\Analyzer\CommentAnalyzer;
use function str_replace;
use function strlen;
use function strpos;
use function strrpos;
use function substr;
use function substr_count;
use function mb_strlen;
use function mb_strpos;
use function mb_strrpos;
use function mb_substr;
use function mb_substr_count;
use function trim;

class CodeLocation
Expand Down Expand Up @@ -156,12 +156,12 @@ private function calculateRealLocation(): void

$file_contents = $codebase->getFileContents($this->file_path);

$file_length = strlen($file_contents);
$file_length = mb_strlen($file_contents);

$search_limit = $this->single_line ? $this->selection_start : $this->selection_end;

if ($search_limit <= $file_length) {
$preview_end = strpos(
$preview_end = mb_strpos(
$file_contents,
"\n",
$search_limit
Expand All @@ -183,7 +183,7 @@ private function calculateRealLocation(): void
) {
$preview_lines = explode(
"\n",
substr(
mb_substr(
$file_contents,
$this->preview_start,
$this->selection_start - $this->preview_start - 1
Expand All @@ -195,7 +195,7 @@ private function calculateRealLocation(): void
$comment_line_offset = $this->docblock_line_number - $this->docblock_start_line_number;

for ($i = 0; $i < $comment_line_offset; ++$i) {
$preview_offset += strlen($preview_lines[$i]) + 1;
$preview_offset += mb_strlen($preview_lines[$i]) + 1;
}

if (!isset($preview_lines[$i])) {
Expand All @@ -206,10 +206,10 @@ private function calculateRealLocation(): void

$indentation = (int)strpos($key_line, '@');

$key_line = trim(preg_replace('@\**/\s*@', '', substr($key_line, $indentation)));
$key_line = trim(preg_replace('@\**/\s*@', '', mb_substr($key_line, $indentation)));

$this->selection_start = $preview_offset + $indentation + $this->preview_start;
$this->selection_end = $this->selection_start + strlen($key_line);
$this->selection_end = $this->selection_start + mb_strlen($key_line);
}

if ($this->regex_type !== null) {
Expand Down Expand Up @@ -258,7 +258,7 @@ private function calculateRealLocation(): void
throw new \UnexpectedValueException('Unrecognised regex type ' . $this->regex_type);
}

$preview_snippet = substr(
$preview_snippet = mb_substr(
$file_contents,
$this->selection_start,
$this->selection_end - $this->selection_start
Expand All @@ -271,15 +271,15 @@ private function calculateRealLocation(): void

if (preg_match($regex, $preview_snippet, $matches, PREG_OFFSET_CAPTURE)) {
$this->selection_start = $this->selection_start + (int)$matches[$match_offset][1];
$this->selection_end = $this->selection_start + strlen((string)$matches[$match_offset][0]);
$this->selection_end = $this->selection_start + mb_strlen((string)$matches[$match_offset][0]);
}
}

// reset preview start to beginning of line
$this->preview_start = (int)strrpos(
$file_contents,
"\n",
min($this->preview_start, $this->selection_start) - strlen($file_contents)
min($this->preview_start, $this->selection_start) - mb_strlen($file_contents)
) + 1;

$this->selection_start = max($this->preview_start, $this->selection_start);
Expand All @@ -289,7 +289,7 @@ private function calculateRealLocation(): void
$this->preview_end = (int)strrpos(
$file_contents,
"\n",
$this->selection_end + 200 - strlen($file_contents)
$this->selection_end + 200 - mb_strlen($file_contents)
);

// if the line is over 200 characters long
Expand All @@ -298,20 +298,20 @@ private function calculateRealLocation(): void
}
}

$this->snippet = substr($file_contents, $this->preview_start, $this->preview_end - $this->preview_start);
$this->text = substr($file_contents, $this->selection_start, $this->selection_end - $this->selection_start);
$this->snippet = mb_substr($file_contents, $this->preview_start, $this->preview_end - $this->preview_start);
$this->text = mb_substr($file_contents, $this->selection_start, $this->selection_end - $this->selection_start);

// reset preview start to beginning of line
$this->column_from = $this->selection_start -
(int)strrpos($file_contents, "\n", $this->selection_start - strlen($file_contents));
(int)strrpos($file_contents, "\n", $this->selection_start - mb_strlen($file_contents));

$newlines = substr_count($this->text, "\n");
$newlines = mb_substr_count($this->text, "\n");

if ($newlines) {
$last_newline_pos = strrpos($file_contents, "\n", $this->selection_end - strlen($file_contents) - 1);
$last_newline_pos = mb_strrpos($file_contents, "\n", $this->selection_end - mb_strlen($file_contents) - 1);
$this->column_to = $this->selection_end - (int)$last_newline_pos;
} else {
$this->column_to = $this->column_from + strlen($this->text);
$this->column_to = $this->column_from + mb_strlen($this->text);
}

$this->end_line_number = $this->getLineNumber() + $newlines;
Expand Down

0 comments on commit e1e0d3c

Please sign in to comment.