MCPcopy Index your code
hub / github.com/Acode-Foundation/Acode / getParserForLanguage

Function getParserForLanguage

src/utils/codeHighlight.js:157–185  ·  view source on GitHub ↗

* Gets language parser by language name (e.g., "javascript", "python") * Uses modelist to find the mode and get first valid extension for file matching

(langName)

Source from the content-addressed store, hash-verified

155 * Uses modelist to find the mode and get first valid extension for file matching
156 */
157async function getParserForLanguage(langName) {
158 if (!langName) return null;
159
160 const modesByName = getModesByName();
161 const normalizedName = langName.toLowerCase();
162
163 // Try to find mode by name (case-insensitive)
164 const mode = modesByName[normalizedName];
165 if (mode?.languageExtension) {
166 try {
167 const langExt = await mode.languageExtension();
168 if (!langExt) return null;
169
170 const langArray = Array.isArray(langExt) ? langExt : [langExt];
171 for (const ext of langArray) {
172 if (ext && typeof ext === "object" && "language" in ext) {
173 return ext.language.parser;
174 }
175 }
176 } catch (e) {
177 console.warn("Failed to get parser for language:", langName, e);
178 }
179 }
180
181 // Fallback: create a fake filename and use getModeForPath
182 // This handles cases where the language name doesn't match mode name exactly
183 const fakeUri = `file.${normalizedName}`;
184 return await getLanguageParser(fakeUri);
185}
186
187/**
188 * Highlights a single line of code for display in references panel

Callers 1

highlightCodeBlockFunction · 0.85

Calls 2

getModesByNameFunction · 0.90
getLanguageParserFunction · 0.85

Tested by

no test coverage detected