Skip to content

[Hosts] Add functionality to remove leading whitespace #37852

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

blakestack
Copy link

@blakestack blakestack commented Mar 9, 2025

Summary of the Pull Request

Add functionality to remove leading whitespace:

  • Add a new bool option
  • Implement the UI for the option in settings app
  • Expose the new option to the Hosts app
  • Use the new option
  • Add test coverage for scenarios where option is on/off

PR Checklist

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

This comment has been minimized.

@htcfreek
Copy link
Collaborator

htcfreek commented Mar 10, 2025

@blakestack
Something strange is happening in your PR. Why are all these null files and the .prettoerrc file?

@blakestack
Copy link
Author

@htcfreek I think these null files are from when I tried to compile PowerToys locally, I accidentally left them in the PR along with the .prettierrc file. I will delete the draft and make a new draft PR. Apart from the null files, am I on the right track with the new feature?

@htcfreek
Copy link
Collaborator

@blakestack
Deleting the files and pushing the changes should be enough. No need to create a new PR.

Why using a drop-down box? I think an on/off toggle is the better choice.

@blakestack
Copy link
Author

@htcfreek Thank you, I will change it to a on/off toggle and then try to add tests

This comment has been minimized.

public UserSettings()
{
_settingsUtils = new SettingsUtils();
ShowStartupWarning = true;
LoopbackDuplicates = false;
AdditionalLinesPosition = HostsAdditionalLinesPosition.Top;
Encoding = HostsEncoding.Utf8;
AddLeadingWhitespace = false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't the current behavior "true"? We shouldn't change that.

Copy link
Author

Choose a reason for hiding this comment

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

@htcfreek I will make these changes now

@@ -29,6 +32,7 @@ public HostsProperties()
ShowStartupWarning = true;
LaunchAdministrator = true;
LoopbackDuplicates = false;
AddLeadingWhitespace = false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

same: true and not false

Comment on lines 162 to 164
else if (anyDisabled) && (AddLeadingWhitespace == false)
{
lineBuilder.Append(e.line);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wondering if this case simply should do nothing and is wrong/obsolete.

Copy link
Author

@blakestack blakestack Mar 11, 2025

Choose a reason for hiding this comment

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

I got confused with the logic here

the case is obsolete. I was thinking the program would hit the more restrictive condition first, and then adjust for the AddLeadingWhitespace value

{
lineBuilder.Append(e.line);
}
else if (anyDisabled)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the case above is obsolete you can use & !AddLeadingWhitespace here.

Copy link
Author

Choose a reason for hiding this comment

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

I will change this and then push the changes now

…update the behavior for adding or removing the Whitespace
@blakestack
Copy link
Author

@htcfreek I made the most recent changes. I am going to try compiling the program and see if it works.

@blakestack
Copy link
Author

I got the program to build, the console gave an error that said the AddLeadingWhite instance variable should be changed to private in the HostsService.cs file. There was also another error that I left trailing whitespaces in the code after deleting my comments, so I deleted the trailing whitespaces. I am not sure how to view the rest of the changes after finishing the build process.

@Jay-o-Way
Copy link
Collaborator

I think an on/off toggle is the better choice.

In general, Toggle Switches are very often misused. They are designed to indicate an explicit ON and OFF state, as in turning a light on or off. The light stands for ACTION or WORK. Button, checkbox, or maybe a dropdown should be used for other cases. Therefore I say a toggleswitch is not appropriate for this situation.

@htcfreek
Copy link
Collaborator

I think an on/off toggle is the better choice.

In general, Toggle Switches are very often misused. They are designed to indicate an explicit ON and OFF state, as in turning a light on or off. The light stands for ACTION or WORK. Button, checkbox, or maybe a dropdown should be used for other cases. Therefore I say a toggleswitch is not appropriate for this situation.

Why? We are turning white spaces on or off.

@yeelam-gordon yeelam-gordon requested a review from Copilot March 31, 2025 05:01
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds functionality to remove leading whitespace from hosts entries by introducing a new boolean option.

  • Adds the new "AddLeadingWhitespace" option in the settings UI and hosts properties.
  • Integrates the option into the HostsService behavior for writing files.
  • Adds test coverage to verify toggling the new whitespace removal functionality.

Reviewed Changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs Added a new property to expose the AddLeadingWhitespace setting.
src/settings-ui/Settings.UI.Library/HostsProperties.cs Introduced the new property with JSON conversion and default initialization.
src/modules/Hosts/HostsUILib/Helpers/HostsService.cs Declared a new field for AddLeadingWhitespace and used it in WriteAsync, but the field is never initialized from user settings.
src/modules/Hosts/Hosts/Settings/UserSettings.cs Added the new property with default value true in user settings initialization.
src/modules/Hosts/Hosts.Tests/HostsServiceTest.cs Added a test case to validate the whitespace removal behavior, though the mock instance appears to be mismatched.
Files not reviewed (1)
  • src/settings-ui/Settings.UI/SettingsXAML/Views/HostsPage.xaml: Language not supported

@blakestack
Copy link
Author

@htcfreek I just made some changes to my PR. I made the changes that were suggested by copilot. Should I continue with the toggle switch implementation?

@blakestack blakestack marked this pull request as ready for review April 1, 2025 01:41
@blakestack blakestack requested a review from htcfreek April 2, 2025 00:21
@htcfreek
Copy link
Collaborator

htcfreek commented Apr 2, 2025

@blakestack
Yes. But I think it makes sense to wait for the PR decision.

@jamrobot jamrobot changed the title Add functionality to remove leading whitespace [Hosts] Add functionality to remove leading whitespace Apr 8, 2025
@jamrobot jamrobot added the Product-Hosts File Editor Refers to the Hosts file editor label Apr 8, 2025
@jamrobot jamrobot requested a review from chenmy77 April 10, 2025 05:54
@jamrobot jamrobot added the Needs-Review This Pull Request awaits the review of a maintainer. label Apr 10, 2025
@chenmy77 chenmy77 added the Don't merge - Hold for release Hold off on merging this PR, even if it's approved. label Apr 25, 2025
@chenmy77 chenmy77 added this to the PowerToys 0.92 milestone Apr 25, 2025
@crutkas crutkas removed the Don't merge - Hold for release Hold off on merging this PR, even if it's approved. label May 15, 2025
@crutkas
Copy link
Member

crutkas commented May 15, 2025

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@blakestack
Copy link
Author

blakestack commented May 19, 2025

I checked the errors from the azure-pipelines bot. I made an additional change to make the 'AddLeadingWhitespace' variable public on line 44 of the HostService.cs file. I believe that should resolve all the errors.

This comment has been minimized.

Copy link

github-actions bot commented Jul 2, 2025

@check-spelling-bot Report

🔴 Please review

See the 📂 files view, the 📜action log, or 📝 job summary for details.

Unrecognized words (39)
advancedpaste
appxpackage
Ashcraft
CDPX
commandnotfound
copyable
Corpor
cropandlock
environmentvariables
fileexploreraddons
filelocksmith
findmymouse
fucntion
fuzzingtesting
hostsfileeditor
hotfixes
IDOn
lcl
LIBFUZZER
makepri
mikeclayton
mousehighlighter
mousejump
mousepointer
mouseutils
MVPs
onebranch
PMs
Psr
quickaccent
regsvr
screenruler
sharpfuzz
sourced
stuttery
textextractor
Windowss
XLoc
zonability
These words are not needed and should be removed cleanmgr CLSCTXINPROCALL CLSCTXLOCALSERVER FILELOCKSMITH IIDI iwr psexec smileys TEXTEXTRACTOR windowsterminal Zhiwei

Some files were automatically ignored 🙈

These sample patterns would exclude them:

^src/common/CalculatorEngineCommon/exprtk\.hpp$

You should consider adding them to:

.github/actions/spell-check/excludes.txt

File matching is via Perl regular expressions.

To check these files, more of their words need to be in the dictionary than not. You can use patterns.txt to exclude portions, add items to the dictionary (e.g. by adding them to allow.txt), or fix typos.

To accept these unrecognized words as correct, update file exclusions, and remove the previously acknowledged and now absent words, you could run the following commands

... in a clone of the [email protected]:blakestack/PowerToys.git repository
on the featureBranch branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/c635c2f3f714eec2fcf27b643a1919b9a811ef2e/apply.pl' |
perl - 'https://github.com/microsoft/PowerToys/actions/runs/16030752704/attempts/1' &&
git commit -m 'Update check-spelling metadata'
Forbidden patterns 🙅 (2)

In order to address this, you could change the content to not match the forbidden patterns (comments before forbidden patterns may help explain why they're forbidden), add patterns for acceptable instances, or adjust the forbidden patterns themselves.

These forbidden patterns matched content:

Do not use (click) here links

For more information, see:

(?i)(?:>|\[)(?:(?:click |)here|this(?=\]\([^\)]+:/)|link|(?:read |)more(?!</value))(?:</|\]\()

Should be greater than

\bhigher than\b
Pattern suggestions ✂️ (1)

You could add these patterns to .github/actions/spell-check/patterns.txt:

# Automatically suggested patterns

# hit-count: 1 file-count: 1
# curl arguments
\b(?:\\n|)curl(?:\.exe|)(?:\s+-[a-zA-Z]{1,2}\b)*(?:\s+-[a-zA-Z]{3,})(?:\s+-[a-zA-Z]+)*

Alternatively, if a pattern suggestion doesn't make sense for this project, add a #
to the beginning of the line in the candidates file with the pattern to stop suggesting it.

Errors, Warnings, and Notices ❌ (4)

See the 📂 files view, the 📜action log, or 📝 job summary for details.

❌ Errors, Warnings, and Notices Count
ℹ️ candidate-pattern 1
❌ check-file-path 20
❌ forbidden-pattern 2
⚠️ large-file 1

See ❌ Event descriptions for more information.

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

@blakestack
Copy link
Author

Is there any way to skip the spellcheck job?

@htcfreek
Copy link
Collaborator

htcfreek commented Jul 2, 2025

Is there any way to skip the spellcheck job?

@blakestack
You can add the false positives to the expected words list. But first you should merge main in.

https://github.com/microsoft/PowerToys/blob/main/.github%2Factions%2Fspell-check%2Fexpect.txt

@crutkas
Copy link
Member

crutkas commented Jul 8, 2025

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs-Review This Pull Request awaits the review of a maintainer. Product-Hosts File Editor Refers to the Hosts file editor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants