Skip to content

Conversation

@mohammed-saalim
Copy link
Contributor

Summary of the Pull Request

Adds a new Hosts File Editor setting “No leading spaces” that prevents prepending spaces to active lines when saving the hosts file (when any entry is disabled). Default is Off to preserve current behavior.

PR Checklist

  • Closes: Hosts File Editor: Adds no whitespace option #36386  

  • Communication: N/A (small, scoped option)

  • Tests: Added/updated and all pass

  • Localization: New en-US strings added; other locales handled by loc pipeline

  • Dev docs: N/A

  • New binaries: None

  • Documentation updated: N/A

Detailed Description of the Pull Request / Additional comments

  • Settings surface:

  - src/settings-ui/Settings.UI.Library/HostsProperties.cs: add NoLeadingSpaces

  - src/modules/Hosts/HostsUILib/Settings/IUserSettings.cs: add NoLeadingSpaces

  - src/modules/Hosts/Hosts/Settings/UserSettings.cs: load/save value from settings.json

  - src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs: expose NoLeadingSpaces

  - src/settings-ui/Settings.UI/SettingsXAML/Views/HostsPage.xaml: new SettingsCard toggle

  - src/settings-ui/Settings.UI/Strings/en-us/Resources.resw: add Hosts_NoLeadingSpaces.Header/Description

  • Writer change:

  - src/modules/Hosts/HostsUILib/Helpers/HostsService.cs: gate indent with anyDisabled && !_userSettings.NoLeadingSpaces

  • Tests:

  - src/modules/Hosts/Hosts.Tests/HostsServiceTest.cs: NoLeadingSpaces_Disabled_RemovesIndent

Backward compatibility: default Off, current formatting unchanged unless the user enables the option.

Validation Steps Performed

  • Automated: HostsEditor.UnitTests including NoLeadingSpaces_Disabled_RemovesIndent passing.

  • Manual:

  1. Run PowerToys (runner) as Admin.

  2. Settings → Hosts File Editor → enable “No leading spaces”.

  3. In editor, add active 127.0.0.10 example1 and disabled 127.0.0.11 example2; Save.

  4. Open C:\Windows\System32\drivers\etc\hosts in Notepad.

     - ON: active line starts at column 0; disabled is # 127....

     - OFF: active line begins with two spaces when a disabled entry exists.

@yeelam-gordon yeelam-gordon added the Product-Hosts File Editor Refers to the Hosts file editor label Aug 19, 2025
@yeelam-gordon yeelam-gordon requested review from Copilot and davidegiacometti and removed request for davidegiacometti August 19, 2025 04:33

This comment was marked as outdated.

@yeelam-gordon
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@yeelam-gordon
Copy link
Contributor

yeelam-gordon commented Aug 19, 2025

@davidegiacometti, I assume originally adding space is for helping Human visual alignment for those IPs.
@mohammed-saalim , I assume you want this one because of "parser"? (i.e. not because of human), and probably even it is an existing parser code you CANNOT change already? (Since trim for heading space usually simple)

@yeelam-gordon yeelam-gordon self-assigned this Aug 19, 2025
@mohammed-saalim
Copy link
Contributor Author

@yeelam-gordon Thanks! Yes, the original two-space indent looks intended for visual alignment. The motivation for this option is parser/tooling compatibility, not human readability. Some consumers expect the IP to start at column 0 and fail or behave unexpectedly when there are leading spaces. Example: squid and a few strict scripts that anchor on ^\d. The Windows sample header even says “The IP address should be placed in the first column”.

Trimming in our own parser wouldn’t help these external tools because they read the file as-is. This PR doesn’t change the default behavior (alignment remains the default); it only adds an opt‑in switch to write active entries without the extra leading spaces when a disabled entry exists. Comments and all other spacing remain unchanged. Before/after:

  • With option ON:
    127.0.0.10 example1
    # 127.0.0.11 example2
    
  • With option OFF (current behavior):
      127.0.0.10 example1
    # 127.0.0.11 example2
    

A unit test (NoLeadingSpaces_Disabled_RemovesIndent) validates the write path.

@davidegiacometti
Copy link
Collaborator

@davidegiacometti, I assume originally adding space is for helping Human visual alignment for those IPs.

Hi, that's correct!

@yeelam-gordon yeelam-gordon merged commit 6130d2a into microsoft:main Aug 19, 2025
9 checks passed
@yeelam-gordon yeelam-gordon added this to the PowerToys 0.94 milestone Aug 19, 2025
@yeelam-gordon yeelam-gordon requested a review from Copilot August 27, 2025 08:02
Copy link
Contributor

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 a new "No leading spaces" option to the Hosts File Editor that prevents prepending spaces to active lines when saving the hosts file. The feature addresses formatting preferences where users want active entries to start at column 0 instead of being indented when disabled entries exist.

  • Adds a new boolean setting NoLeadingSpaces with default value false to preserve current behavior
  • Modifies the hosts file writing logic to conditionally apply leading spaces based on the new setting
  • Includes comprehensive UI integration and test coverage for the new functionality

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
HostsProperties.cs Adds NoLeadingSpaces property with default false value
IUserSettings.cs Extends interface to include NoLeadingSpaces property
UserSettings.cs Implements NoLeadingSpaces property loading from settings
HostsViewModel.cs Exposes NoLeadingSpaces property for UI binding
HostsPage.xaml Adds toggle switch UI control for the new setting
Resources.resw Adds localization strings for the new setting
HostsService.cs Modifies write logic to honor NoLeadingSpaces setting
HostsServiceTest.cs Adds unit test to verify NoLeadingSpaces functionality

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Product-Hosts File Editor Refers to the Hosts file editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hosts File Editor: Adds no whitespace option

3 participants