- Entry Point: The extension starts with
src/extension.ts, which is the main file executed when VSCode activates the extension. - Activation Event: The extension is activated when a Go file (
.go) is opened in VSCode, as defined inpackage.jsonunderactivationEvents(e.g.,"onLanguage:go"). - Setup:
- Registers a Completion Provider for the Go language using the
vscode.languages.registerCompletionItemProviderAPI. - Specifies trigger characters (e.g., ```,
, `=`, `;`, `,`) from `src/constants/trigger_chars.const.ts` to initiate or re-trigger suggestions. - Loads tag definitions and suggestions from modular files in
src/tags/(e.g.,json.tag.ts,gorm.tag.ts,validate.tag.ts) andsrc/completion/suggestion/libraries/(e.g.,json.suggest.ts,gorm.suggest.ts).
- Registers a Completion Provider for the Go language using the
- User Action: The user opens a Go file and starts typing a struct definition, e.g.:
type User struct { Name string `
- Trigger Detection: When the user types a backtick (```), the extension detects it as a trigger character (defined in
trigger_chars.const.ts).
- Flow:
- The
Completion Providerdefined insrc/completion/completion.provider.tsis invoked. - The extension checks if the cursor is within a Go struct using utility functions like
check_struct.util.ts. - If confirmed, it fetches a list of supported tag types (e.g.,
json:"",gorm:"",validate:"") fromsrc/completion/suggestion/library.suggest.ts.
- The
- Output: A dropdown appears with suggestions like:
json:""gorm:""validate:""- Other supported libraries (e.g.,
bson:"",xml:"", etc.).
- Details: Each suggestion is a
CompletionItem(created viacompletion_item.util.ts) with a label, description, and documentation in Markdown format.
- User Action: The user selects a tag, e.g.,
json:"", resulting in:type User struct { Name string `json:""`
- Flow:
- The extension uses regex-based parsing (via
check_quote.util.ts) to detect that the cursor is inside double quotes (""). - It identifies the tag type (
json) and fetches context-specific suggestions fromsrc/completion/suggestion/libraries/json.suggest.ts. - Suggestions are dynamically generated based on the library (e.g.,
omitempty,string,-forjson).
- The extension uses regex-based parsing (via
- Output: A new dropdown appears with options like:
omitemptystring-
- Documentation: Hovering over a suggestion (e.g.,
omitempty) displays detailed Markdown documentation (e.g., "Omits the field from JSON output if its value is empty").
- User Action: The user types a space, equal sign, semicolon, or comma inside the quotes, e.g.:
type User struct { Name string `json:"name "`
- Flow:
- The extension detects the trigger character (
,=,;,,) usingtrigger_chars.const.ts. - It re-invokes the
Completion Providerto provide additional suggestions based on the current tag context. - For example, after
json:"name ", it might suggestomitemptyagain if not already added.
- The extension detects the trigger character (
- Output: Suggestions reappear, allowing the user to chain multiple tag values (e.g.,
json:"name,omitempty").
- Flow:
- The extension uses
field_name.util.tsto extract the struct field name (e.g.,Name) and converts it to a suggested tag value usingcase_conversion.util.ts. - For
json:"", it might suggestname(lowercase) based on common JSON conventions.
- The extension uses
- Output: The user can accept the suggestion to complete the tag:
type User struct { Name string `json:"name"`
- User Action: The user adds another backtick to include additional tags:
type User struct { Name string `json:"name" `
- Flow:
- The extension detects the new backtick and suggests additional tag types (e.g.,
gorm:"",validate:""). - The process repeats, providing context-aware suggestions for the new tag.
- The extension detects the new backtick and suggests additional tag types (e.g.,
- Output: The user can build a multi-tag struct field:
type User struct { Name string `json:"name,omitempty" gorm:"column:name" validate:"required"`
- Flow:
- When the user hovers over a completed tag (e.g.,
json:"name,omitempty"), the extension provides a hover provider (likely inextension.tsor a separate provider) that fetches documentation from the respective library file (e.g.,json.tag.ts).
- When the user hovers over a completed tag (e.g.,
- Output: A tooltip displays:
- Label:
omitempty - Description: "Omits the field from JSON output if its value is empty (e.g., zero value for the type)."
- Example: ```go
type User struct {
Name string
json:"name,omitempty"} // If Name is "", it will be omitted from JSON.
- Label:
- Invalid Context: If the backtick is typed outside a struct,
check_struct.util.tsprevents suggestions from appearing. - Performance: The lightweight design ensures minimal lag, even in large files, by optimizing regex and suggestion generation.
- Extensibility: New libraries can be added by creating files in
src/tags/andsrc/completion/suggestion/libraries/, following the modular structure.
- User Action: The user finishes editing the struct and saves the file.
- Flow:
- The extension does not persist state between sessions but re-evaluates the context each time a trigger character is typed.
- Suggestions remain available as long as the file is open and the cursor is in a valid position.
type User struct {
Name string
Age int
}type User struct {
Name string `
Age int
}- Suggestions:
json:"",gorm:"",validate:"", etc.
type User struct {
Name string `json:""`
Age int
}- Suggestions:
name,omitempty,string,-.
type User struct {
Name string `json:"name "`
Age int
}- Suggestions:
omitempty,string,-.
type User struct {
Name string `json:"name,omitempty" `
Age int
}- Suggestions:
gorm:"",validate:"", etc.
type User struct {
Name string `json:"name,omitempty" gorm:"column:name" validate:"required"`
Age int
}- Hovering over any tag shows detailed documentation.
- Initialization:
extension.tsregisters theCompletion Provider. - Trigger Detection: Backtick (```) or other characters (
, `=`, `;`, `,`) trigger `completion.provider.ts`. - Context Check:
check_struct.util.tsandcheck_quote.util.tsvalidate the cursor position. - Suggestion Generation: Library-specific files (e.g.,
json.suggest.ts) provide dynamic suggestions. - Item Creation:
completion_item.util.tsbuildsCompletionItemobjects with documentation. - Re-triggering: Additional characters re-invoke the provider for chained suggestions.
- Documentation: Hover provider displays Markdown-formatted details.
This flow leverages the modular file structure and ensures GoTagMate is intuitive for beginners while powerful for advanced users, aligning with its design goals.