<commit_before>local math_floor = math.floor
local string_gmatch = string.gmatch
local string_gsub = string.gsub
local draw_WordBox = draw.WordBox

local EDITOR = {}

-- CPU hint box
local oldpos, haschecked = {0,0}, false
function EDITOR:Think()
  local caret = self:CursorToCaret()
  local startpos, word = self:getWordStart( caret, true )

  if word and word ~= "" then
    if not haschecked then
      oldpos = {startpos[1],startpos[2]}
      haschecked = true
      timer.Simple(0.3,function()
        if not self then return end
        if not self.CursorToCaret then return end
        local caret = self:CursorToCaret()
        local startpos, word = self:getWordStart( caret, true )
        if startpos[1] == oldpos[1] and startpos[2] == oldpos[2] then
          self.CurrentVarValue = { startpos, word }
        end
      end)
    elseif (oldpos[1] ~= startpos[1] or oldpos[2] ~= startpos[2]) and haschecked then
      haschecked = false
      self.CurrentVarValue = nil
      oldpos = {0,0}
    end
  else
    self.CurrentVarValue = nil
    haschecked = false
    oldpos = {0,0}
  end
end

local colors = {
  ["normal"]   = { Color(255, 255, 136), false},
  ["opcode"]   = { Color(255, 136,   0), false},
  ["comment"]  = { Color(128, 128, 128), false},
  ["register"] = { Color(255, 255, 136), false},
  ["number"]   = { Color(232, 232,   0), false},
  ["string"]   = { Color(255, 136, 136), false},
  ["filename"] = { Color(232, 232, 232), false},
  ["label"]    = { Color(255, 255, 176), false},
  ["keyword"]  = { Color(255, 136,   0), false},
  ["memref"]   = { Color(232, 232,   0), false},
  ["pmacro"]   = { Color(136, 136, 255), false},
  ["error"]    = { Color(240,  96,  96), false},
  --		["compare"]  = { Color(255, 186,  40), true},
}

-- Build lookup table for opcodes
local opcodeTable = {}
for k,v in pairs(CPULib.InstructionTable) do
  if v.Mnemonic ~= "RESERVED" then
    opcodeTable[v.Mnemonic] = true
  end
end

-- Build lookup table for keywords
local keywordsList = {
  "GOTO","FOR","IF","ELSE","WHILE","DO","SWITCH","CASE","CONST","RETURN","BREAK",
  "CONTINUE","EXPORT","INLINE","FORWARD","REGISTER","DB","ALLOC","SCALAR","VECTOR1F",
  "VECTOR2F","UV","VECTOR3F","VECTOR4F","COLOR","VEC1F","VEC2F","VEC3F","VEC4F","MATRIX",
  "STRING","DB","DEFINE","CODE","DATA","ORG","OFFSET","INT48","FLOAT","CHAR","VOID",
  "INT","FLOAT","CHAR","VOID","PRESERVE","ZAP","STRUCT","VECTOR"
}

local keywordsTable = {}
for k,v in pairs(keywordsList) do
  keywordsTable[v] = true
end

-- Build lookup table for registers
local registersTable = {
  EAX = true,EBX = true,ECX = true,EDX = true,ESI = true,EDI = true,
  ESP = true,EBP = true,CS = true,SS = true,DS = true,ES = true,GS = true,
  FS = true,KS = true,LS = true
}
for reg=0,31 do registersTable["R"..reg] = true end
for port=0,1023 do registersTable["PORT"..port] = true end

-- Build lookup table for macros
local macroTable = {
  ["PRAGMA"] = true,
  ["INCLUDE"] = true,
  ["#INCLUDE##"] = true,
  ["DEFINE"] = true,
  ["IFDEF"] = true,
  ["IFNDEF"] = true,
  ["ENDIF"] = true,
  ["ELSE"] = true,
  ["UNDEF"] = true,
}

function EDITOR:CommentSelection(removecomment)
  local comment_char = "//"
  if removecomment then
    -- shift-TAB with a selection --
    local tmp = string_gsub("\n"..self:GetSelection(), "\n"..comment_char, "\n")

    -- makes sure that the first line is outdented
    self:SetSelection(tmp:sub(2))
  else
    -- plain TAB with a selection --
    self:SetSelection(comment_char .. self:GetSelection():gsub("\n", "\n"..comment_char))
  end
end

function EDITOR:BlockCommentSelction(removecomment)
  local sel_start, sel_caret = self:MakeSelection( self:Selection() )
  local str = self:GetSelection()<commit_msg>Fix ZCPU editor "Comment Selection" not working<commit_after>local math_floor = math.floor
local string_gmatch = string.gmatch
local string_gsub = string.gsub
local draw_WordBox = draw.WordBox

local EDITOR = {}

-- CPU hint box
local oldpos, haschecked = {0,0}, false
function EDITOR:Think()
  local caret = self:CursorToCaret()
  local startpos, word = self:getWordStart( caret, true )

  if word and word ~= "" then
    if not haschecked then
      oldpos = {startpos[1],startpos[2]}
      haschecked = true
      timer.Simple(0.3,function()
        if not self then return end
        if not self.CursorToCaret then return end
        local caret = self:CursorToCaret()
        local startpos, word = self:getWordStart( caret, true )
        if startpos[1] == oldpos[1] and startpos[2] == oldpos[2] then
          self.CurrentVarValue = { startpos, word }
        end
      end)
    elseif (oldpos[1] ~= startpos[1] or oldpos[2] ~= startpos[2]) and haschecked then
      haschecked = false
      self.CurrentVarValue = nil
      oldpos = {0,0}
    end
  else
    self.CurrentVarValue = nil
    haschecked = false
    oldpos = {0,0}
  end
end

local colors = {
  ["normal"]   = { Color(255, 255, 136), false},
  ["opcode"]   = { Color(255, 136,   0), false},
  ["comment"]  = { Color(128, 128, 128), false},
  ["register"] = { Color(255, 255, 136), false},
  ["number"]   = { Color(232, 232,   0), false},
  ["string"]   = { Color(255, 136, 136), false},
  ["filename"] = { Color(232, 232, 232), false},
  ["label"]    = { Color(255, 255, 176), false},
  ["keyword"]  = { Color(255, 136,   0), false},
  ["memref"]   = { Color(232, 232,   0), false},
  ["pmacro"]   = { Color(136, 136, 255), false},
  ["error"]    = { Color(240,  96,  96), false},
  --		["compare"]  = { Color(255, 186,  40), true},
}

-- Build lookup table for opcodes
local opcodeTable = {}
for k,v in pairs(CPULib.InstructionTable) do
  if v.Mnemonic ~= "RESERVED" then
    opcodeTable[v.Mnemonic] = true
  end
end

-- Build lookup table for keywords
local keywordsList = {
  "GOTO","FOR","IF","ELSE","WHILE","DO","SWITCH","CASE","CONST","RETURN","BREAK",
  "CONTINUE","EXPORT","INLINE","FORWARD","REGISTER","DB","ALLOC","SCALAR","VECTOR1F",
  "VECTOR2F","UV","VECTOR3F","VECTOR4F","COLOR","VEC1F","VEC2F","VEC3F","VEC4F","MATRIX",
  "STRING","DB","DEFINE","CODE","DATA","ORG","OFFSET","INT48","FLOAT","CHAR","VOID",
  "INT","FLOAT","CHAR","VOID","PRESERVE","ZAP","STRUCT","VECTOR"
}

local keywordsTable = {}
for k,v in pairs(keywordsList) do
  keywordsTable[v] = true
end

-- Build lookup table for registers
local registersTable = {
  EAX = true,EBX = true,ECX = true,EDX = true,ESI = true,EDI = true,
  ESP = true,EBP = true,CS = true,SS = true,DS = true,ES = true,GS = true,
  FS = true,KS = true,LS = true
}
for reg=0,31 do registersTable["R"..reg] = true end
for port=0,1023 do registersTable["PORT"..port] = true end

-- Build lookup table for macros
local macroTable = {
  ["PRAGMA"] = true,
  ["INCLUDE"] = true,
  ["#INCLUDE##"] = true,
  ["DEFINE"] = true,
  ["IFDEF"] = true,
  ["IFNDEF"] = true,
  ["ENDIF"] = true,
  ["ELSE"] = true,
  ["UNDEF"] = true,
}

function EDITOR:CommentSelection(removecomment)
  local comment_char = "//"
  if removecomment then
    -- shift-TAB with a selection --
    local tmp = string_gsub("\n"..self:GetSelection(), "\n"..comment_char, "\n")

    -- makes sure that the first line is outdented
    self:SetSelection(tmp:sub(2))
  else
    -- plain TAB with a selection --
    self:SetSelection(comment_char .. self:GetSelection():gsub("\n", "\n"..comment_char))
  end
end

function EDITOR:BlockCommentSelection(removecomment)
  local sel_start, sel_caret = self:MakeSelection( self:Selection() )
  local str = self:GetSelection()
{'fraction_non_alphanumeric': 0.17704566760412482, 'fraction_numerical': 0.0383018615240391, 'mean_word_length': 3.7536600891152134, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6070813', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2614, 'n_words': 651}