summaryrefslogtreecommitdiffhomepage
path: root/internal/lsp/discovery/language.go
blob: 25fe17d5518de4023d9bb480b97f47ab6178583b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package discovery

import (
	"os"
	"path/filepath"
	"strings"
	"sync"

	"github.com/sst/opencode/internal/lsp"
	"log/slog"
)

// LanguageInfo stores information about a detected language
type LanguageInfo struct {
	// Language identifier (e.g., "go", "typescript", "python")
	ID string

	// Number of files detected for this language
	FileCount int

	// Project files associated with this language (e.g., go.mod, package.json)
	ProjectFiles []string

	// Whether this is likely a primary language in the project
	IsPrimary bool
}

// ProjectFile represents a project configuration file
type ProjectFile struct {
	// File name or pattern to match
	Name string

	// Associated language ID
	LanguageID string

	// Whether this file strongly indicates the language is primary
	IsPrimary bool
}

// Common project files that indicate specific languages
var projectFilePatterns = []ProjectFile{
	{Name: "go.mod", LanguageID: "go", IsPrimary: true},
	{Name: "go.sum", LanguageID: "go", IsPrimary: false},
	{Name: "package.json", LanguageID: "javascript", IsPrimary: true}, // Could be TypeScript too
	{Name: "tsconfig.json", LanguageID: "typescript", IsPrimary: true},
	{Name: "jsconfig.json", LanguageID: "javascript", IsPrimary: true},
	{Name: "pyproject.toml", LanguageID: "python", IsPrimary: true},
	{Name: "setup.py", LanguageID: "python", IsPrimary: true},
	{Name: "requirements.txt", LanguageID: "python", IsPrimary: true},
	{Name: "Cargo.toml", LanguageID: "rust", IsPrimary: true},
	{Name: "Cargo.lock", LanguageID: "rust", IsPrimary: false},
	{Name: "CMakeLists.txt", LanguageID: "cmake", IsPrimary: true},
	{Name: "pom.xml", LanguageID: "java", IsPrimary: true},
	{Name: "build.gradle", LanguageID: "java", IsPrimary: true},
	{Name: "build.gradle.kts", LanguageID: "kotlin", IsPrimary: true},
	{Name: "composer.json", LanguageID: "php", IsPrimary: true},
	{Name: "Gemfile", LanguageID: "ruby", IsPrimary: true},
	{Name: "Rakefile", LanguageID: "ruby", IsPrimary: true},
	{Name: "mix.exs", LanguageID: "elixir", IsPrimary: true},
	{Name: "rebar.config", LanguageID: "erlang", IsPrimary: true},
	{Name: "dune-project", LanguageID: "ocaml", IsPrimary: true},
	{Name: "stack.yaml", LanguageID: "haskell", IsPrimary: true},
	{Name: "cabal.project", LanguageID: "haskell", IsPrimary: true},
	{Name: "Makefile", LanguageID: "make", IsPrimary: false},
	{Name: "Dockerfile", LanguageID: "dockerfile", IsPrimary: false},
}

// Map of file extensions to language IDs
var extensionToLanguage = map[string]string{
	".go":    "go",
	".js":    "javascript",
	".jsx":   "javascript",
	".ts":    "typescript",
	".tsx":   "typescript",
	".py":    "python",
	".rs":    "rust",
	".java":  "java",
	".c":     "c",
	".cpp":   "cpp",
	".h":     "c",
	".hpp":   "cpp",
	".rb":    "ruby",
	".php":   "php",
	".cs":    "csharp",
	".fs":    "fsharp",
	".swift": "swift",
	".kt":    "kotlin",
	".scala": "scala",
	".hs":    "haskell",
	".ml":    "ocaml",
	".ex":    "elixir",
	".exs":   "elixir",
	".erl":   "erlang",
	".lua":   "lua",
	".r":     "r",
	".sh":    "shell",
	".bash":  "shell",
	".zsh":   "shell",
	".html":  "html",
	".css":   "css",
	".scss":  "scss",
	".sass":  "sass",
	".less":  "less",
	".json":  "json",
	".xml":   "xml",
	".yaml":  "yaml",
	".yml":   "yaml",
	".md":    "markdown",
	".dart":  "dart",
}

// Directories to exclude from scanning
var excludedDirs = map[string]bool{
	".git":         true,
	"node_modules": true,
	"vendor":       true,
	"dist":         true,
	"build":        true,
	"target":       true,
	".idea":        true,
	".vscode":      true,
	".github":      true,
	".gitlab":      true,
	"__pycache__":  true,
	".next":        true,
	".nuxt":        true,
	"venv":         true,
	"env":          true,
	".env":         true,
}

// DetectLanguages scans a directory to identify programming languages used in the project
func DetectLanguages(rootDir string) (map[string]LanguageInfo, error) {
	languages := make(map[string]LanguageInfo)
	var mutex sync.Mutex

	// Walk the directory tree
	err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return nil // Skip files that can't be accessed
		}

		// Skip excluded directories
		if info.IsDir() {
			if excludedDirs[info.Name()] || strings.HasPrefix(info.Name(), ".") {
				return filepath.SkipDir
			}
			return nil
		}

		// Skip hidden files
		if strings.HasPrefix(info.Name(), ".") {
			return nil
		}

		// Check for project files
		for _, pattern := range projectFilePatterns {
			if info.Name() == pattern.Name {
				mutex.Lock()
				lang, exists := languages[pattern.LanguageID]
				if !exists {
					lang = LanguageInfo{
						ID:           pattern.LanguageID,
						FileCount:    0,
						ProjectFiles: []string{},
						IsPrimary:    pattern.IsPrimary,
					}
				}
				lang.ProjectFiles = append(lang.ProjectFiles, path)
				if pattern.IsPrimary {
					lang.IsPrimary = true
				}
				languages[pattern.LanguageID] = lang
				mutex.Unlock()
				break
			}
		}

		// Check file extension
		ext := strings.ToLower(filepath.Ext(path))
		if langID, ok := extensionToLanguage[ext]; ok {
			mutex.Lock()
			lang, exists := languages[langID]
			if !exists {
				lang = LanguageInfo{
					ID:           langID,
					FileCount:    0,
					ProjectFiles: []string{},
				}
			}
			lang.FileCount++
			languages[langID] = lang
			mutex.Unlock()
		}

		return nil
	})

	if err != nil {
		return nil, err
	}

	// Determine primary languages based on file count if not already marked
	determinePrimaryLanguages(languages)

	// Log detected languages
	for id, info := range languages {
		if info.IsPrimary {
			slog.Debug("Detected primary language", "language", id, "files", info.FileCount, "projectFiles", len(info.ProjectFiles))
		} else {
			slog.Debug("Detected secondary language", "language", id, "files", info.FileCount)
		}
	}

	return languages, nil
}

// determinePrimaryLanguages marks languages as primary based on file count
func determinePrimaryLanguages(languages map[string]LanguageInfo) {
	// Find the language with the most files
	var maxFiles int
	for _, info := range languages {
		if info.FileCount > maxFiles {
			maxFiles = info.FileCount
		}
	}

	// Mark languages with at least 20% of the max files as primary
	threshold := max(maxFiles/5, 5) // At least 5 files to be considered primary

	for id, info := range languages {
		if !info.IsPrimary && info.FileCount >= threshold {
			info.IsPrimary = true
			languages[id] = info
		}
	}
}

// GetLanguageIDFromExtension returns the language ID for a given file extension
func GetLanguageIDFromExtension(ext string) string {
	ext = strings.ToLower(ext)
	if langID, ok := extensionToLanguage[ext]; ok {
		return langID
	}
	return ""
}

// GetLanguageIDFromProtocol converts a protocol.LanguageKind to our language ID string
func GetLanguageIDFromProtocol(langKind string) string {
	// Convert protocol language kind to our language ID
	switch langKind {
	case "go":
		return "go"
	case "typescript":
		return "typescript"
	case "typescriptreact":
		return "typescript"
	case "javascript":
		return "javascript"
	case "javascriptreact":
		return "javascript"
	case "python":
		return "python"
	case "rust":
		return "rust"
	case "java":
		return "java"
	case "c":
		return "c"
	case "cpp":
		return "cpp"
	default:
		// Try to normalize the language kind
		return strings.ToLower(langKind)
	}
}

// GetLanguageIDFromPath determines the language ID from a file path
func GetLanguageIDFromPath(path string) string {
	// Check file extension first
	ext := filepath.Ext(path)
	if langID := GetLanguageIDFromExtension(ext); langID != "" {
		return langID
	}

	// Check if it's a known project file
	filename := filepath.Base(path)
	for _, pattern := range projectFilePatterns {
		if filename == pattern.Name {
			return pattern.LanguageID
		}
	}

	// Use LSP's detection as a fallback
	uri := "file://" + path
	langKind := lsp.DetectLanguageID(uri)
	return GetLanguageIDFromProtocol(string(langKind))
}