<commit_before>local M = {}

local state = {
    tags = nil,   -- current set of tags
    tagstack = {},-- current tag stack: list of { i=num, tags={tag list} }
    tagidx = 0,   -- index into tagstack of current level
    lasttag = nil,-- last tag list
}
M.state = state

-- Load a tags file
local function load_tags()
    local tagf = io.open("tags") -- TODO: configurable tags file location
    local results = {}
    local pat = "^([^\t]*)\t([^\t]*)\t(.*)$"
    for line in tagf:lines() do
        local tname, fname, excmd = line:match(pat)
        local flags
        if tname then
          -- Initialise to an empty list if necessary
          if not results[tname] then results[tname] = {} end
          -- And append.
          local l = results[tname]  -- now guaranteed to be a table
          
          do
             -- Try to separate the ex command from extension fields
             local e,f = excmd:match('^(.-);"\t(.*)$')
             if e then
                 excmd = e
                 flags = f
             end
          end
          l[#l+1] = { sym=tname, filename=fname, excmd=excmd, flags=flags }
        end
    end
    tagf:close()
    state.tags = results
end

-- Return or load the tags
local function get_tags()
    -- TODO: check if tags file needs reloading
    if state.tags == nil then
        load_tags()
    end
    return state.tags
end

function M.find_tag_exact(name)
    local tags = get_tags()
    local result = tags[name]
    if result then
        local newidx = state.tagidx + 1
        
        -- Clear the stack above where we are.
        while newidx <= #state.tagstack do
            state.tagstack[#state.tagstack] = nil
        end
        
        state.tagstack[newidx] = {
           i=1,         -- which tag within this list
           tags=result, -- this level's tags
           fromname=buffer.filename,  -- where we came from
           frompos=buffer.current_pos,-- where we came from
        }
        state.lasttag = result
        state.tagidx = newidx<commit_msg>Don't try to read from a non-existant tags file.<commit_after>local M = {}

local state = {
    tags = nil,   -- current set of tags
    tagstack = {},-- current tag stack: list of { i=num, tags={tag list} }
    tagidx = 0,   -- index into tagstack of current level
    lasttag = nil,-- last tag list
}
M.state = state

-- Load a tags file
local function load_tags()
    local tagf = io.open("tags") -- TODO: configurable tags file location
    local results = {}
    local pat = "^([^\t]*)\t([^\t]*)\t(.*)$"
    if tagf then
      for line in tagf:lines() do
          local tname, fname, excmd = line:match(pat)
          local flags
          if tname then
            -- Initialise to an empty list if necessary
            if not results[tname] then results[tname] = {} end
            -- And append.
            local l = results[tname]  -- now guaranteed to be a table
          
            do
               -- Try to separate the ex command from extension fields
               local e,f = excmd:match('^(.-);"\t(.*)$')
               if e then
                   excmd = e
                   flags = f
               end
            end
            l[#l+1] = { sym=tname, filename=fname, excmd=excmd, flags=flags }
          end
      end
      tagf:close()
    end
    state.tags = results
end

-- Return or load the tags
local function get_tags()
    -- TODO: check if tags file needs reloading
    if state.tags == nil then
        load_tags()
    end
    return state.tags
end

function M.find_tag_exact(name)
    local tags = get_tags()
    local result = tags[name]
    if result then
        local newidx = state.tagidx + 1
        
        -- Clear the stack above where we are.
        while newidx <= #state.tagstack do
            state.tagstack[#state.tagstack] = nil
        end
        
        state.tagstack[newidx] = {
           i=1,         -- which tag within this list
           tags=result, -- this level's tags
           fromname=buffer.filename,  -- where we came from
           frompos=buffer.current_pos,-- where we came from
        }
        state.lasttag = result
        state.tagidx = newidx
{'fraction_non_alphanumeric': 0.10330776605944392, 'fraction_numerical': 0.0019175455417066154, 'mean_word_length': 1.6630504148053606, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3335095', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1195, 'n_words': 419}