File tree Expand file tree Collapse file tree 8 files changed +35
-14
lines changed
Expand file tree Collapse file tree 8 files changed +35
-14
lines changed Original file line number Diff line number Diff line change @@ -139,10 +139,10 @@ export class Config {
139139
140140 /**
141141 * Check if the formatter is enabled.
142- * @returns true if formatter is enabled (default) , false if explicitly disabled
142+ * @returns true if formatter is explicitly enabled , false otherwise (default)
143143 */
144144 public get isFormatterEnabled ( ) : boolean {
145- return this . config . formatter ?. enabled ?? Config . getDefaultConfig ( ) . formatter ?. enabled ?? true
145+ return this . config . formatter ?. enabled ?? Config . getDefaultConfig ( ) . formatter ?. enabled ?? false
146146 }
147147
148148 /**
@@ -1096,7 +1096,7 @@ export class Config {
10961096 rules : { }
10971097 } ,
10981098 formatter : {
1099- enabled : true ,
1099+ enabled : false ,
11001100 indentWidth : 2 ,
11011101 maxLineLength : 80
11021102 }
Original file line number Diff line number Diff line change @@ -379,10 +379,10 @@ describe("@herb-tools/config", () => {
379379 expect ( config . isFormatterEnabled ) . toBe ( false )
380380 } )
381381
382- test ( "isFormatterEnabled returns true by default" , ( ) => {
382+ test ( "isFormatterEnabled returns false by default" , ( ) => {
383383 const config = Config . fromObject ( { } , { projectPath : testDir } )
384384
385- expect ( config . isFormatterEnabled ) . toBe ( true )
385+ expect ( config . isFormatterEnabled ) . toBe ( false )
386386 } )
387387
388388 test ( "isRuleDisabled returns true when rule is disabled" , ( ) => {
Original file line number Diff line number Diff line change @@ -171,17 +171,18 @@ export class CLI {
171171 }
172172
173173 const config = await Config . loadForCLI ( configFile || startPath , version )
174+ const hasConfigFile = Config . exists ( config . projectPath )
174175 const formatterConfig = config . formatter || { }
175176
176- if ( formatterConfig . enabled === false && ! isForceMode ) {
177+ if ( hasConfigFile && formatterConfig . enabled === false && ! isForceMode ) {
177178 console . log ( "Formatter is disabled in .herb.yml configuration." )
178179 console . log ( "To enable formatting, set formatter.enabled: true in .herb.yml" )
179180 console . log ( "Or use --force to format anyway." )
180181
181182 process . exit ( 0 )
182183 }
183184
184- if ( isForceMode && formatterConfig . enabled === false ) {
185+ if ( isForceMode && hasConfigFile && formatterConfig . enabled === false ) {
185186 console . error ( "⚠️ Forcing formatter run (disabled in .herb.yml)" )
186187 console . error ( )
187188 }
Original file line number Diff line number Diff line change @@ -18,12 +18,31 @@ export class DocumentSaveService {
1818 this . formattingService = formattingService
1919 }
2020
21+ /**
22+ * Apply only autofix edits on save.
23+ * Called by willSaveWaitUntil - formatting is handled separately by editor.formatOnSave
24+ */
25+ async applyFixes ( document : TextDocument ) : Promise < TextEdit [ ] > {
26+ const settings = await this . settings . getDocumentSettings ( document . uri )
27+ const fixOnSave = settings ?. linter ?. fixOnSave !== false
28+
29+ this . connection . console . log ( `[DocumentSave] applyFixes fixOnSave=${ fixOnSave } ` )
30+
31+ if ( ! fixOnSave ) return [ ]
32+
33+ return this . autofixService . autofix ( document )
34+ }
35+
36+ /**
37+ * Apply autofix and formatting.
38+ * Called by onDocumentFormatting (manual format or editor.formatOnSave)
39+ */
2140 async applyFixesAndFormatting ( document : TextDocument , reason : TextDocumentSaveReason ) : Promise < TextEdit [ ] > {
2241 const settings = await this . settings . getDocumentSettings ( document . uri )
2342 const fixOnSave = settings ?. linter ?. fixOnSave !== false
2443 const formatterEnabled = settings ?. formatter ?. enabled ?? false
2544
26- this . connection . console . log ( `[DocumentSave] fixOnSave=${ fixOnSave } , formatterEnabled=${ formatterEnabled } ` )
45+ this . connection . console . log ( `[DocumentSave] applyFixesAndFormatting fixOnSave=${ fixOnSave } , formatterEnabled=${ formatterEnabled } ` )
2746
2847 let autofixEdits : TextEdit [ ] = [ ]
2948
Original file line number Diff line number Diff line change @@ -210,6 +210,9 @@ export class FormattingService {
210210 if ( filePath . endsWith ( '.herb.yml' ) ) return false
211211 if ( ! this . config ) return true
212212
213+ const hasConfigFile = Config . exists ( this . config . projectPath )
214+ if ( ! hasConfigFile ) return true
215+
213216 const relativePath = filePath . replace ( 'file://' , '' ) . replace ( this . project . projectPath + '/' , '' )
214217
215218 return this . config . isFormatterEnabledForPath ( relativePath )
Original file line number Diff line number Diff line change @@ -12,15 +12,12 @@ import {
1212 DocumentRangeFormattingParams ,
1313 CodeActionParams ,
1414 CodeActionKind ,
15- TextEdit ,
1615} from "vscode-languageserver/node"
1716
1817import { Service } from "./service"
1918import { PersonalHerbSettings } from "./settings"
2019import { Config } from "@herb-tools/config"
2120
22- import type { TextDocument } from "vscode-languageserver-textdocument"
23-
2421export class Server {
2522 private service ! : Service
2623 private connection : Connection
@@ -37,7 +34,7 @@ export class Server {
3734 await this . service . init ( )
3835
3936 this . service . documentService . documents . onWillSaveWaitUntil ( async ( event ) => {
40- return this . service . documentSaveService . applyFixesAndFormatting ( event . document , event . reason )
37+ return this . service . documentSaveService . applyFixes ( event . document )
4138 } )
4239
4340 const result : InitializeResult = {
Original file line number Diff line number Diff line change @@ -87,8 +87,9 @@ export class Settings {
8787 // TODO: ideally we can just use Config all the way through
8888 private mergeSettings ( userSettings : PersonalHerbSettings | null , projectConfig ?: Config ) : PersonalHerbSettings {
8989 const settings = userSettings || this . defaultSettings
90+ const hasConfigFile = projectConfig ? Config . exists ( projectConfig . projectPath ) : false
9091
91- if ( ! projectConfig ) {
92+ if ( ! projectConfig || ! hasConfigFile ) {
9293 return {
9394 trace : settings . trace ,
9495 linter : {
Original file line number Diff line number Diff line change @@ -274,7 +274,7 @@ describe('DocumentSaveService', () => {
274274 )
275275
276276 expect ( connection . console . log ) . toHaveBeenCalledWith (
277- '[DocumentSave] fixOnSave=true, formatterEnabled=true'
277+ '[DocumentSave] applyFixesAndFormatting fixOnSave=true, formatterEnabled=true'
278278 )
279279 } )
280280 } )
You can’t perform that action at this time.
0 commit comments