mirror of
https://github.com/lukaszraczylo/filepuff-mcp.git
synced 2026-06-10 22:59:01 +00:00
Ho hum.
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
// Package protocol defines shared types used across the MCP file operations server.
|
||||
package protocol
|
||||
|
||||
// Location represents a position in a file.
|
||||
type Location struct {
|
||||
File string `json:"file"`
|
||||
Line int `json:"line"`
|
||||
Column int `json:"column"`
|
||||
}
|
||||
|
||||
// Range represents a range in a file.
|
||||
type Range struct {
|
||||
Start Location `json:"start"`
|
||||
End Location `json:"end"`
|
||||
}
|
||||
|
||||
// SymbolKind represents the kind of a symbol.
|
||||
type SymbolKind string
|
||||
|
||||
const (
|
||||
SymbolFunction SymbolKind = "function"
|
||||
SymbolMethod SymbolKind = "method"
|
||||
SymbolClass SymbolKind = "class"
|
||||
SymbolStruct SymbolKind = "struct"
|
||||
SymbolInterface SymbolKind = "interface"
|
||||
SymbolVariable SymbolKind = "variable"
|
||||
SymbolConstant SymbolKind = "constant"
|
||||
SymbolType SymbolKind = "type"
|
||||
SymbolField SymbolKind = "field"
|
||||
SymbolProperty SymbolKind = "property"
|
||||
SymbolModule SymbolKind = "module"
|
||||
SymbolPackage SymbolKind = "package"
|
||||
)
|
||||
|
||||
// Symbol represents a code symbol (function, class, variable, etc.).
|
||||
type Symbol struct {
|
||||
Name string `json:"name"`
|
||||
Kind SymbolKind `json:"kind"`
|
||||
Doc string `json:"doc,omitempty"`
|
||||
Location Location `json:"location"`
|
||||
}
|
||||
|
||||
// SyntaxError represents a syntax error in a file.
|
||||
type SyntaxError struct {
|
||||
Message string `json:"message"`
|
||||
Location Location `json:"location"`
|
||||
}
|
||||
|
||||
// Language represents a programming language.
|
||||
type Language string
|
||||
|
||||
const (
|
||||
LangGo Language = "go"
|
||||
LangTypeScript Language = "typescript"
|
||||
LangJavaScript Language = "javascript"
|
||||
LangPython Language = "python"
|
||||
LangC Language = "c"
|
||||
LangCpp Language = "cpp"
|
||||
LangHTML Language = "html"
|
||||
LangVue Language = "vue"
|
||||
LangJSON Language = "json"
|
||||
LangYAML Language = "yaml"
|
||||
LangUnknown Language = "unknown"
|
||||
)
|
||||
|
||||
// DetectLanguage detects the language from a filename.
|
||||
func DetectLanguage(filename string) Language {
|
||||
ext := getExtension(filename)
|
||||
switch ext {
|
||||
case ".go":
|
||||
return LangGo
|
||||
case ".ts", ".tsx":
|
||||
return LangTypeScript
|
||||
case ".js", ".jsx", ".mjs", ".cjs":
|
||||
return LangJavaScript
|
||||
case ".py", ".pyw":
|
||||
return LangPython
|
||||
case ".c", ".h":
|
||||
return LangC
|
||||
case ".cpp", ".cc", ".cxx", ".hpp", ".hxx":
|
||||
return LangCpp
|
||||
case ".html", ".htm":
|
||||
return LangHTML
|
||||
case ".vue":
|
||||
return LangVue
|
||||
case ".json":
|
||||
return LangJSON
|
||||
case ".yaml", ".yml":
|
||||
return LangYAML
|
||||
default:
|
||||
return LangUnknown
|
||||
}
|
||||
}
|
||||
|
||||
func getExtension(filename string) string {
|
||||
for i := len(filename) - 1; i >= 0; i-- {
|
||||
if filename[i] == '.' {
|
||||
return filename[i:]
|
||||
}
|
||||
if filename[i] == '/' || filename[i] == '\\' {
|
||||
break
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package protocol
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDetectLanguage(t *testing.T) {
|
||||
tests := []struct {
|
||||
filename string
|
||||
expected Language
|
||||
}{
|
||||
{"main.go", LangGo},
|
||||
{"server.go", LangGo},
|
||||
{"index.ts", LangTypeScript},
|
||||
{"component.tsx", LangTypeScript},
|
||||
{"Button.tsx", LangTypeScript},
|
||||
{"app.js", LangJavaScript},
|
||||
{"component.jsx", LangJavaScript},
|
||||
{"Component.jsx", LangJavaScript},
|
||||
{"module.mjs", LangJavaScript},
|
||||
{"common.cjs", LangJavaScript},
|
||||
{"script.py", LangPython},
|
||||
{"app.pyw", LangPython},
|
||||
{"main.c", LangC},
|
||||
{"header.h", LangC},
|
||||
{"main.cpp", LangCpp},
|
||||
{"main.cc", LangCpp},
|
||||
{"main.cxx", LangCpp},
|
||||
{"header.hpp", LangCpp},
|
||||
{"header.hxx", LangCpp},
|
||||
{"index.html", LangHTML},
|
||||
{"page.htm", LangHTML},
|
||||
{"Component.vue", LangVue},
|
||||
{"unknown.txt", LangUnknown},
|
||||
{"README", LangUnknown},
|
||||
{"path/to/file.go", LangGo},
|
||||
{"path/to/file.ts", LangTypeScript},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.filename, func(t *testing.T) {
|
||||
result := DetectLanguage(tt.filename)
|
||||
if result != tt.expected {
|
||||
t.Errorf("DetectLanguage(%q) = %q, want %q", tt.filename, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetExtension(t *testing.T) {
|
||||
tests := []struct {
|
||||
filename string
|
||||
expected string
|
||||
}{
|
||||
{"file.go", ".go"},
|
||||
{"file.test.go", ".go"},
|
||||
{"path/to/file.ts", ".ts"},
|
||||
{"noextension", ""},
|
||||
{".hidden", ".hidden"},
|
||||
{"file.", "."},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.filename, func(t *testing.T) {
|
||||
result := getExtension(tt.filename)
|
||||
if result != tt.expected {
|
||||
t.Errorf("getExtension(%q) = %q, want %q", tt.filename, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user