init.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. -- Copyright 2007-2017 Mitchell mitchell.att.foicica.com. See LICENSE.
  2. local M = {}
  3. --[[ This comment is for LuaDoc.
  4. ---
  5. -- The php module.
  6. -- It provides utilities for editing PHP code.
  7. module('_M.php')]]
  8. -- Load HTML Autocompletion and documentation.
  9. if not _M.html then _M.html = require('html') end
  10. -- Sets default buffer properties for PHP files.
  11. events.connect(events.LEXER_LOADED, function(lang)
  12. if lang == 'php' then
  13. buffer.word_chars =
  14. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_'
  15. end
  16. end)
  17. ---
  18. -- List of ctags files to use for autocompletion.
  19. -- @class table
  20. -- @name tags
  21. M.tags = {_HOME..'/modules/php/tags', _USERHOME..'/modules/php/tags'}
  22. -- Autocompletion and documentation.
  23. local completion = '%s'..string.char(buffer.auto_c_type_separator)..'%d'
  24. local XPM = textadept.editing.XPM_IMAGES
  25. local xpms = {
  26. c = XPM.CLASS, f = XPM.METHOD, m = XPM.VARIABLE, M = XPM.STRUCT,
  27. v = XPM.VARIABLE
  28. }
  29. textadept.editing.autocompleters.php = function()
  30. local list = {}
  31. -- Retrieve the symbol behind the caret.
  32. local line, pos = buffer:get_cur_line()
  33. local symbol, op, part = line:sub(1, pos):match('([%w_%.]-)(%.?)([%w_]*)$')
  34. if symbol == '' and part == '' then return nil end -- nothing to complete
  35. -- Attempt to identify the symbol type.
  36. -- TODO: identify literals like "'foo'." and "[1, 2, 3].".
  37. local buffer = buffer
  38. local assignment = '%f[%w_]'..symbol:gsub('(%p)', '%%%1')..'%s*=%s*(.*)$'
  39. for i = buffer:line_from_position(buffer.current_pos) - 1, 0, -1 do
  40. local expr = buffer:get_line(i):match(assignment)
  41. if expr then
  42. for patt, type in pairs(M.expr_types) do
  43. if expr:find(patt) then symbol = type break end
  44. end
  45. if expr:find('^[%u][%w_.]*%s*%b()%s*$') then
  46. symbol = expr:match('^([%u][%w_.]+)%s*%b()%s*$') -- e.g. a = Foo()
  47. break
  48. end
  49. end
  50. end
  51. -- Search through ctags for completions for that symbol.
  52. local name_patt = '^'..part
  53. local sep = string.char(buffer.auto_c_type_separator)
  54. for i = 1, #M.tags do
  55. if lfs.attributes(M.tags[i]) then
  56. for line in io.lines(M.tags[i]) do
  57. local name = line:match('^%S+')
  58. if name:find(name_patt) and not list[name] then
  59. local fields = line:match(';"\t(.*)$')
  60. local k, class = fields:sub(1, 1), fields:match('class:(%S+)') or ''
  61. if class == symbol and (op ~= ':' or k == 'f') then
  62. list[#list + 1] = ("%s%s%d"):format(name, sep, xpms[k])
  63. list[name] = true
  64. end
  65. end
  66. end
  67. end
  68. end
  69. return #part, list
  70. end
  71. textadept.editing.api_files.php = {
  72. _HOME..'/modules/php/api', _USERHOME..'/modules/php/api'
  73. }
  74. -- Commands.
  75. ---
  76. -- Container for PHP-specific key bindings.
  77. -- @class table
  78. -- @name _G.keys.php
  79. keys.php = {
  80. ['s\n'] = function()
  81. buffer:line_end()
  82. buffer:add_text(';')
  83. buffer:new_line()
  84. end,
  85. }
  86. -- Snippets.
  87. if type(snippets) == 'table' then
  88. ---
  89. -- Container for PHP-specific snippets.
  90. -- @class table
  91. -- @name _G.snippets.php
  92. snippets.php = {
  93. ['php'] = '<?php\n%0\n?>',
  94. ['if'] = 'if (%1(logic)) {\n\t%0(# code...)\n}',
  95. ['for'] = 'for ($%1(i)=0; $%1 < %2; $%1++) {\n\t%0(# code...)\n}',
  96. ['foreach'] = 'foreach ($%1(variable) as $%2(key)%3( => %4($value))) {\n\t%0(# code...)\n}',
  97. ['echo'] = "echo '%1(test)';%0",
  98. ['function'] = 'function %1(function_name)(%2) {\n\t%0(# code...)\n}',
  99. ['class'] = 'class %1(AClass) %2(extends %3(AnotherClass)) {\n\tfunction __construct(%4(argument)) {\n\t\t%0(// code)\n\t}\n}',
  100. ['/**'] = '/**\n * %0\n */',
  101. }
  102. end
  103. return M