todo

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2025 License: MIT Imports: 39 Imported by: 0

README

TODO

A library for parsing structured TODO comments out of 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"
	"context"

	"github.com/icholy/todo"
)

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

	// parse todos
	ctx := context.Background()
	todos, _ := todo.Parse(ctx, 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 LanguageFor

func LanguageFor(file string) (*sitter.Language, bool)

LanguageFor returns the language for the given file name

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 Location

type Location struct {
	File string
	Line int
}

Location represents a file location

type Todo

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

Todo represents a TODO line

func Parse

func Parse(ctx context.Context, file string, source []byte) ([]Todo, error)

Parse parses the source and returns all TODO comments.

func ParseCode

func ParseCode(ctx context.Context, file string, source []byte, lang *sitter.Language) ([]Todo, error)

ParseCode parses the source code and returns all TODO comments

func ParseLine

func ParseLine(line string) (Todo, bool)

ParseLine parses a single TODO line. Does not set the Location or Line fields.

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