import { isNumber } from '@sindresorhus/is'; import type { Props } from 'editorconfig'; import { parse } from 'editorconfig'; import upath from 'upath'; import { GlobalConfig } from '../../config/global.ts'; import { logger } from '../../logger/index.ts'; import type { CodeFormat } from './code-format.ts'; import type { IndentationType } from './indentation-type.ts'; export class EditorConfig { public static async getCodeFormat(fileName: string): Promise { const localDir = GlobalConfig.get('localDir'); try { const knownProps = await parse(upath.join(localDir, fileName)); return { indentationSize: EditorConfig.getIndentationSize(knownProps), indentationType: EditorConfig.getIndentationType(knownProps), maxLineLength: knownProps.max_line_length as number | 'off' | undefined, }; } catch (err) { logger.warn({ err }, 'Failed to parse editor config'); return {}; } } private static getIndentationType( knownProps: Props, ): IndentationType | undefined { const { indent_style: indentStyle } = knownProps; if (indentStyle === 'tab') { return 'tab'; } if (indentStyle === 'space') { return 'space'; } return undefined; } private static getIndentationSize(knownProps: Props): number | undefined { const { indent_size: indentSize } = knownProps; if (isNumber(indentSize) && Number.isInteger(indentSize)) { return indentSize; } return undefined; } }