-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SCR #0 - add generating all docblock
- Loading branch information
Nikita Tsurkan
committed
Aug 27, 2023
1 parent
6934610
commit d914efc
Showing
11 changed files
with
287 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
test.php | ||
getterSetter.php | ||
phpTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
local M = {} | ||
|
||
-- Основная функция для генерации докблоков | ||
function M.generateAllDocblock() | ||
local ts_utils = require 'nvim-treesitter.ts_utils' | ||
local bufnr = vim.api.nvim_get_current_buf() | ||
-- local getMethodNode = require('lua.docblock.lib.getMethodNode') | ||
-- local cursor_node = ts_utils.get_node_at_cursor() | ||
local parser = vim.treesitter.get_parser(bufnr, 'php') | ||
local root = parser:parse()[1] | ||
|
||
local function findClassDeclaration(node) | ||
if node == nil then | ||
return nil | ||
end | ||
|
||
if node:type() == 'class_declaration' then | ||
return node | ||
else | ||
findClassDeclaration(node:child()) | ||
end | ||
end | ||
|
||
local class = nil | ||
|
||
for node in root:root():iter_children() do | ||
class = findClassDeclaration(node) | ||
|
||
if class ~= nil then | ||
break | ||
end | ||
end | ||
|
||
if class ~= nil then | ||
local getMethodNode = require('docblock.lib.getMethodNode') | ||
local methodNodesTable = getMethodNode.getMethodNode(class) | ||
local iterableCountDocblock = 0 | ||
|
||
local tableNodeWithParamsAndReturnTypes = {} | ||
|
||
-- Получили все методы | ||
for _, node in ipairs(methodNodesTable) do | ||
local formal_module = require('docblock.lib.getFormalParametrsForMethod') | ||
local formalParametrs = formal_module.getFormalParametrsForMethod(node)[1] | ||
local methodParametrs = {} | ||
local methodReturnTypes = {} | ||
|
||
methodParametrs = formalParametrs | ||
|
||
local return_module = require('docblock.lib.getReturnTypesForMethod') | ||
local returnTypes = return_module.getReturnTypesForMethod(node) | ||
|
||
for _, returns in ipairs(returnTypes) do | ||
methodReturnTypes = returns | ||
end | ||
|
||
table.insert(tableNodeWithParamsAndReturnTypes, {node = node, parametrs = methodParametrs, returnTypes = methodReturnTypes}) | ||
end | ||
|
||
for _, node in ipairs(tableNodeWithParamsAndReturnTypes) do | ||
local printer = require('docblock.lib.printDocblock') | ||
iterableCountDocblock = printer.print(bufnr, node.node, node.parametrs, node.returnTypes, iterableCountDocblock) | ||
end | ||
end | ||
|
||
end | ||
|
||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
local M = {} | ||
local function iterableFormalParametrs(node) | ||
local tableParamets = {} | ||
for child in node:iter_children() do | ||
if child:type() == 'simple_parameter' then | ||
local type = child:field('type')[1] | ||
local name = child:field('name')[1] | ||
|
||
if type ~= nil then | ||
type = vim.treesitter.get_node_text(type, 0) | ||
end | ||
|
||
if name ~= nil then | ||
name = vim.treesitter.get_node_text(name, 0) | ||
end | ||
|
||
table.insert(tableParamets, {type = type, name = name}) | ||
end | ||
end | ||
|
||
return tableParamets | ||
end | ||
|
||
function M.getFormalParametrsForMethod(node) | ||
local parametrs = {} | ||
|
||
local allParametrs = node:field('parameters') | ||
|
||
-- print(#allParametrs) | ||
for _, nodeParametrs in pairs(allParametrs) do | ||
-- formal_parametrs | ||
local params = iterableFormalParametrs(nodeParametrs) | ||
table.insert(parametrs, params) | ||
end | ||
|
||
return parametrs | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
local M = {} | ||
|
||
function M.getMethodNode(node) | ||
local tableNodesMethod = {} | ||
|
||
local function getMethod(my_node) | ||
for child in my_node:iter_children() do | ||
if child:type() == 'method_declaration' then | ||
local prevNode = child:prev_sibling() | ||
if prevNode ~= nil then | ||
if prevNode:type() == 'comment' then | ||
goto continue | ||
end | ||
end | ||
|
||
table.insert(tableNodesMethod, child) | ||
else | ||
getMethod(child) | ||
end | ||
::continue:: | ||
end | ||
|
||
return tableNodesMethod | ||
end | ||
|
||
tableNodesMethod = getMethod(node) | ||
return tableNodesMethod | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
local M = {} | ||
local function iterableReturnTypes(node) | ||
local tableReturns = {} | ||
|
||
for child in node:iter_children() do | ||
if child:type() == 'primitive_type' then | ||
local returnType = vim.treesitter.get_node_text(child, 0) | ||
|
||
table.insert(tableReturns, returnType) | ||
end | ||
end | ||
|
||
return tableReturns | ||
end | ||
|
||
function M.getReturnTypesForMethod(node) | ||
local returnsTable = {} | ||
|
||
local allReturnTypes = node:field('return_type') | ||
|
||
for _, returnType in pairs(allReturnTypes) do | ||
local returns = iterableReturnTypes(returnType) | ||
table.insert(returnsTable, returns) | ||
end | ||
|
||
return returnsTable | ||
end | ||
|
||
return M | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
local M = {} | ||
|
||
local function generateDocblock(method, parameters, returnTypes) | ||
local docblock = { | ||
' /**', | ||
} | ||
|
||
for _, parametr in pairs(parameters) do | ||
table.insert( | ||
docblock, ' * param ' | ||
.. (parametr.type ~= nil and parametr.type .. ' ' or '') | ||
.. (parametr.name ~= nil and parametr.name or '') | ||
) | ||
end | ||
|
||
if returnTypes == nil then | ||
return | ||
end | ||
|
||
if next(returnTypes) ~= nil then | ||
local countReturnTypes = #returnTypes | ||
local returnString = '' | ||
|
||
if countReturnTypes > 1 then | ||
returnString = table.concat(returnTypes, '|') | ||
else | ||
returnString = returnTypes[1] | ||
end | ||
|
||
table.insert( | ||
docblock, ' * return ' .. returnString | ||
) | ||
end | ||
|
||
table.insert(docblock, ' */') | ||
|
||
return docblock | ||
end | ||
|
||
function M.print(bufnr, method, parameters, returnTypes, iterableCountDocblock) | ||
local docblock = generateDocblock(method, parameters, returnTypes) | ||
|
||
if docblock == nil then | ||
return 0 | ||
end | ||
|
||
vim.api.nvim_buf_set_lines(bufnr, method:start() + iterableCountDocblock, method:start() + iterableCountDocblock, false, docblock) | ||
|
||
iterableCountDocblock = iterableCountDocblock + #docblock | ||
|
||
return iterableCountDocblock | ||
end | ||
|
||
return M | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
local M = {} | ||
|
||
function M.makeDocblock(property, method) | ||
local docblock = { | ||
' /**', | ||
} | ||
|
||
if method == 'getter' then | ||
if property.type ~= '' then | ||
table.insert(docblock, ' * @return ' .. property.type) | ||
else | ||
table.insert(docblock, ' * @return mixed') | ||
end | ||
end | ||
|
||
if method == 'setter' then | ||
if property.type ~= '' then | ||
table.insert(docblock, ' * @param ' .. property.type .. ' $value') | ||
table.insert(docblock, ' * @return void') | ||
else | ||
table.insert(docblock, ' * @param mixed $value') | ||
table.insert(docblock, ' * @return void') | ||
end | ||
end | ||
|
||
table.insert(docblock, ' */') | ||
|
||
return docblock | ||
end | ||
|
||
return M |
Oops, something went wrong.