todo

package module
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2025 License: MIT Imports: 26 Imported by: 0

README

TODO

A library for parsing structured TODO comments from code.

Go Reference

Overview

  • Tree-Sitter is used to parse comments from source files.
  • A simple recursive descent parser then extracts and interprets TODO lines.

Syntax

TODO can appear anywhere in a comment line; anything before TODO is ignored. Once TODO is found:

  1. An optional comma-separated list of attributes can follow, enclosed in parentheses.
  2. A colon (:) must appear next.
  3. Everything after the colon is the description.
Examples
// TODO: no attributes
// TODO(foo,bar): two key-only attributes
// TODO(created=2025-03-09, assigned=john): multiple key/value
// TODO(deadline="June 2025"): quoted value 
Grammar
todo-line  ::= (any text) "TODO" [ "(" attributes? ")" ] ":" description
attributes ::= attribute [ "," attribute ]*
attribute  ::= bare-key | key-value
key-value  ::= bare-key "=" (bare-key | quoted-value)
description ::= (any text to end of line)
bare-key   ::= (any non-whitespace sequence without parentheses, commas, or '=')
quoted-value ::= "\"" (any text) "\""

API Usage

package main

import (
	"os"
	"fmt"

	"github.com/icholy/todo"
)

func main() {
	// read file source
	file := "./main.go"
	source, _ := os.ReadFile(source)

	// parse todos
	todos, _ := todo.Parse(file, source)

	// print todos with deadlines
	for _, t := range todos {
		if _, ok := t.Attribute("deadline"); ok {
			fmt.Println(t)
		}
	}
}

CLI Tool

A minimal CLI tool is provided to parse and output these comments as JSON.

Installation
go install github.com/icholy/todo/cmd/todo@latest
Usage Example
todo ./**/*.go
./todo.go:88 TODO: investigate compilation error

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterLanguage added in v0.0.7

func RegisterLanguage(opt LanguageOptions)

RegisterLanguage registers a language with the given extensions. If no queries are provided, the default queries will be used.

Types

type Attribute

type Attribute struct {
	Key   string
	Value string
	Quote bool
}

Attribute represents a key=value pair.

func (Attribute) String added in v0.0.2

func (a Attribute) String() string

String returns a string representation.

type LanguageOptions added in v0.0.8

type LanguageOptions struct {
	Name       string
	Extensions []string
	Language   *treesitter.Language
	Queries    []*treesitter.Query
}

LanguageOptions are treesitter options for a language.

func LanguageFor

func LanguageFor(file string) (*LanguageOptions, bool)

LanguageFor returns the language for the given file name.

type Location

type Location struct {
	File string
	Line int
}

Location represents a file location.

func (Location) String added in v0.0.6

func (l Location) String() string

String returns a string representation of the location.

type Todo

type Todo struct {
	Line        string
	Location    Location
	Description string
	Attributes  []Attribute
}

Todo represents a TODO line.

func Parse

func Parse(file string, source []byte) ([]Todo, error)

Parse parses the source and returns all TODO comments.

func ParseCode

func ParseCode(file string, source []byte, opt *LanguageOptions) ([]Todo, error)

ParseCode parses the source code and returns all TODO comments. If lang is nil, the language is inferred from the file extension.

func ParseText

func ParseText(file string, text []byte) []Todo

ParseText parses a text string and returns all TODO comments.

func (Todo) Attribute added in v0.0.3

func (t Todo) Attribute(key string) (string, bool)

Attribute returns the value for the given key.

func (Todo) String added in v0.0.2

func (t Todo) String() string

String returns a string representation.

Directories

Path Synopsis
cmd
todo command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL