Skip to content

Commit

Permalink
Format Selection No Longer Limited To First Character (#50)
Browse files Browse the repository at this point in the history
* Ensured All Messages Are Filtered

This alters the position mapping so that it encompasses the length of an entire token. This ensures formatting requests will work when any part of a problem token is selected.
  • Loading branch information
ObliviousHarmony authored Jul 29, 2022
1 parent 5ea4b7e commit 92342f8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 39 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.6.0] - 2022-05-06
### Fixed
- Document formatting with no changes clears diagnostics.
- Document selection formatting only works on the first character of the diagnostic.

## [1.6.0] - 2022-05-06
### Fixed
Expand Down
69 changes: 32 additions & 37 deletions assets/phpcs-integration/Extension/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,40 +203,31 @@ public function fixCodeAction($sourceStackPtr, $source)
return $this->getTextEdits($changedTokens);
}

public function addFixableError($error, $stackPtr, $code, $data = array(), $severity = 0)
{
if (isset($this->codeActionToken)) {
// We will assume that the error can be recorded because it wouldn't be in here otherwise.
return $this->codeActionToken === $stackPtr && $this->codeActionSource === $code;
}

// Check the format range if one is set.
if (isset($this->formatStartToken) && $stackPtr < $this->formatStartToken) {
return false;
}
if (isset($this->formatEndToken) && $stackPtr > $this->formatEndToken) {
return false;
}

return parent::addFixableError($error, $stackPtr, $code, $data, $severity);
}

public function addFixableWarning($warning, $stackPtr, $code, $data = array(), $severity = 0)
/**
* Attempts to record a message if we aren't actively ignoring it.
*
* @inheritDoc
*/
protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable)
{
if (isset($this->codeActionToken)) {
// We will assume that the error can be recorded because it wouldn't be in here otherwise.
return $this->codeActionToken === $stackPtr && $this->codeActionSource === $code;
}
// Check to see if we're only looking for a specific subset of messages.
$stackPtr = $this->getStackPtrForPosition($line, $column);
if (isset($stackPtr)) {
if (isset($this->codeActionToken)) {
// We will assume that the error can be recorded because it wouldn't be in here otherwise.
return $this->codeActionToken === $stackPtr && $this->codeActionSource === $code;
}

// Check the format range if one is set.
if (isset($this->formatStartToken) && $stackPtr < $this->formatStartToken) {
return false;
}
if (isset($this->formatEndToken) && $stackPtr > $this->formatEndToken) {
return false;
// Check the format range if one is set.
if (isset($this->formatStartToken) && $stackPtr < $this->formatStartToken) {
return false;
}
if (isset($this->formatEndToken) && $stackPtr > $this->formatEndToken) {
return false;
}
}

return parent::addFixableWarning($warning, $stackPtr, $code, $data, $severity);
return parent::addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable);
}

/**
Expand Down Expand Up @@ -264,12 +255,11 @@ private function prepareTokensForVSCode()
// to make the data easier to work with.
if (isset($token['orig_content'])) {
$columnWidth = mb_strlen($token['orig_content']);
$endsWithNewline = substr($token['orig_content'], -1);
$endsWithNewline = substr($token['orig_content'], -1) === "\n";
} else {
$columnWidth = $token['length'];
$endsWithNewline = substr($token['content'], -1);
$endsWithNewline = substr($token['content'], -1) === "\n";
}
$endsWithNewline = $endsWithNewline === "\n" || $endsWithNewline === "\r\n";

$originalColumn = $column - $columnOffset;

Expand All @@ -294,10 +284,15 @@ private function prepareTokensForVSCode()
// Store the range object to use elsewhere.
$this->tokens[$stackPtr]['vscode_range'] = $range;

// Make it easy to find the specific token associated with a position.
$this->tokenPositionMap[$line][$column] = $stackPtr;
// We will also store the range position for convenience.
$this->tokenPositionMap[$range['startLine'] . ':' . $range['startCharacter']] = $stackPtr;
// Create a map to convert from a line/character position to a token pointer.
for ($mapPos = $column; $mapPos <= $column + $token['length']; $mapPos++) {
$this->tokenPositionMap[$line][$mapPos] = $stackPtr;
}

// Do the same with range positions.
for ($mapPos = $range['startCharacter']; $mapPos <= $range['startCharacter'] + $columnWidth; ++$mapPos) {
$this->tokenPositionMap[$range['startLine'] . ':' . $mapPos] = $stackPtr;
}

// Our offset is the difference between the old and new lengths.
$columnOffset += $token['length'] - $columnWidth;
Expand Down

0 comments on commit 92342f8

Please sign in to comment.