1package main
  2
  3// Supported file types, their extensions, and language-specific settings like
  4// indentation and LSP commands.
  5
  6import "path/filepath"
  7
  8// FileType represents the configuration for a specific programming language.
  9type FileType struct {
 10	Name             string   // Display name of the file type.
 11	Extensions       []string // File extensions (e.g., .go, .py) or filenames (e.g., Makefile).
 12	UseTabs          bool     // Whether to use tabs for indentation.
 13	Comment          string   // Single-line comment prefix (e.g., // or #).
 14	TabWidth         int      // Number of spaces for a tab.
 15	EnableLSP        bool     // Whether to enable Language Server Protocol support.
 16	LSPCommand       string   // Executable name of the LSP server.
 17	LSPCommandArgs   []string // Arguments to pass to the LSP server.
 18	FormatterCommand string   // External command for formatting the file.
 19}
 20
 21// fileTypes is a global list of all supported languages in the editor.
 22var fileTypes = []*FileType{
 23	{
 24		Name:       "Go",
 25		Extensions: []string{".go"},
 26		UseTabs:    true,
 27		Comment:    "//",
 28		TabWidth:   Config.DefaultTabWidth,
 29		EnableLSP:  true,
 30		LSPCommand: "gopls",
 31	},
 32	{
 33		Name:       "C",
 34		Extensions: []string{".c", ".h"},
 35		UseTabs:    true,
 36		Comment:    "//",
 37		TabWidth:   Config.DefaultTabWidth,
 38		EnableLSP:  true,
 39		LSPCommand: "clangd",
 40	},
 41	{
 42		Name:       "C++",
 43		Extensions: []string{".cpp", ".hpp", ".cc", ".hh", ".cxx", ".hxx"},
 44		UseTabs:    true,
 45		Comment:    "//",
 46		TabWidth:   Config.DefaultTabWidth,
 47		EnableLSP:  true,
 48		LSPCommand: "clangd",
 49	},
 50	{
 51		Name:           "JavaScript",
 52		Extensions:     []string{".js"},
 53		UseTabs:        true,
 54		Comment:        "//",
 55		TabWidth:       Config.DefaultTabWidth,
 56		EnableLSP:      true,
 57		LSPCommand:     "typescript-language-server",
 58		LSPCommandArgs: []string{"--stdio"},
 59	},
 60	{
 61		Name:           "TypeScript",
 62		Extensions:     []string{".ts"},
 63		UseTabs:        true,
 64		Comment:        "//",
 65		TabWidth:       Config.DefaultTabWidth,
 66		EnableLSP:      true,
 67		LSPCommand:     "typescript-language-server",
 68		LSPCommandArgs: []string{"--stdio"},
 69	},
 70	{
 71		Name:           "TSX",
 72		Extensions:     []string{".tsx"},
 73		UseTabs:        true,
 74		Comment:        "//",
 75		TabWidth:       Config.DefaultTabWidth,
 76		EnableLSP:      true,
 77		LSPCommand:     "typescript-language-server",
 78		LSPCommandArgs: []string{"--stdio"},
 79	},
 80	{
 81		Name:           "Python",
 82		Extensions:     []string{".py"},
 83		UseTabs:        false,
 84		Comment:        "#",
 85		TabWidth:       Config.DefaultTabWidth,
 86		LSPCommand:     "pyright-langserver",
 87		LSPCommandArgs: []string{"--stdio"},
 88	},
 89	{
 90		Name:       "Bash",
 91		Extensions: []string{".sh"},
 92		UseTabs:    true,
 93		Comment:    "#",
 94		TabWidth:   Config.DefaultTabWidth,
 95	},
 96	{
 97		Name:       "CSS",
 98		Extensions: []string{".css"},
 99		UseTabs:    false,
100		Comment:    "//",
101		TabWidth:   Config.DefaultTabWidth,
102	},
103	{
104		Name:       "Dockerfile",
105		Extensions: []string{".dockerfile", "Dockerfile"},
106		UseTabs:    false,
107		Comment:    "#",
108		TabWidth:   Config.DefaultTabWidth,
109	},
110	{
111		Name:       "HTML",
112		Extensions: []string{".html", ".htm"},
113		UseTabs:    false,
114		Comment:    "",
115		TabWidth:   Config.DefaultTabWidth,
116	},
117	{
118		Name:       "Lua",
119		Extensions: []string{".lua"},
120		UseTabs:    true,
121		Comment:    "--",
122		TabWidth:   Config.DefaultTabWidth,
123	},
124	{
125		Name:       "Markdown",
126		Extensions: []string{".md", ".markdown"},
127		UseTabs:    false,
128		Comment:    "",
129		TabWidth:   Config.DefaultTabWidth,
130	},
131	{
132		Name:       "PHP",
133		Extensions: []string{".php"},
134		UseTabs:    true,
135		Comment:    "//",
136		TabWidth:   Config.DefaultTabWidth,
137	},
138	{
139		Name:       "SQL",
140		Extensions: []string{".sql"},
141		UseTabs:    true,
142		Comment:    "--",
143		TabWidth:   Config.DefaultTabWidth,
144	},
145	{
146		Name:       "Makefile",
147		Extensions: []string{".make", "Makefile", "makefile"},
148		UseTabs:    true,
149		Comment:    "#",
150		TabWidth:   Config.DefaultTabWidth,
151	},
152	{
153		Name:       "Text",
154		Extensions: []string{},
155		UseTabs:    false,
156		Comment:    "",
157		TabWidth:   Config.DefaultTabWidth,
158	},
159}
160
161// getFileType detects the file type based on the filename or extension.
162func getFileType(filename string) *FileType {
163	ext := filepath.Ext(filename)
164	base := filepath.Base(filename)
165	for _, ft := range fileTypes {
166		for _, e := range ft.Extensions {
167			// Check if the extension matches or if the base filename (like 'Makefile') matches.
168			if e == ext || e == base {
169				return ft
170			}
171		}
172	}
173	// Return Default file type (Text) if no match is found.
174	return fileTypes[len(fileTypes)-1]
175}
176
177// InitFileTypes resets language settings to the current global configuration.
178func InitFileTypes() {
179	for _, ft := range fileTypes {
180		ft.TabWidth = Config.DefaultTabWidth
181	}
182}