|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Solargraph |
| 4 | + module Convention |
| 5 | + module DataDefinition |
| 6 | + autoload :DataDefintionNode, 'solargraph/convention/data_definition/data_definition_node' |
| 7 | + autoload :DataAssignmentNode, 'solargraph/convention/data_definition/data_assignment_node' |
| 8 | + |
| 9 | + module NodeProcessors |
| 10 | + class DataNode < Parser::NodeProcessor::Base |
| 11 | + # @return [Boolean] continue processing the next processor of the same node. |
| 12 | + def process |
| 13 | + return true if data_definition_node.nil? |
| 14 | + |
| 15 | + loc = get_node_location(node) |
| 16 | + nspin = Solargraph::Pin::Namespace.new( |
| 17 | + type: :class, |
| 18 | + location: loc, |
| 19 | + closure: region.closure, |
| 20 | + name: data_definition_node.class_name, |
| 21 | + comments: comments_for(node), |
| 22 | + visibility: :public, |
| 23 | + gates: region.closure.gates.freeze |
| 24 | + ) |
| 25 | + pins.push nspin |
| 26 | + |
| 27 | + # define initialize method |
| 28 | + initialize_method_pin = Pin::Method.new( |
| 29 | + name: 'initialize', |
| 30 | + parameters: [], |
| 31 | + scope: :instance, |
| 32 | + location: get_node_location(node), |
| 33 | + closure: nspin, |
| 34 | + visibility: :private, |
| 35 | + comments: comments_for(node) |
| 36 | + ) |
| 37 | + |
| 38 | + # TODO: Support both arg and kwarg initializers for Data.define |
| 39 | + # Solargraph::SourceMap::Clip#complete_keyword_parameters does not seem to currently take into account [Pin::Method#signatures] hence we only one for :kwarg |
| 40 | + pins.push initialize_method_pin |
| 41 | + |
| 42 | + data_definition_node.attributes.map do |attribute_node, attribute_name| |
| 43 | + initialize_method_pin.parameters.push( |
| 44 | + Pin::Parameter.new( |
| 45 | + name: attribute_name, |
| 46 | + decl: :kwarg, |
| 47 | + location: get_node_location(attribute_node), |
| 48 | + closure: initialize_method_pin |
| 49 | + ) |
| 50 | + ) |
| 51 | + end |
| 52 | + |
| 53 | + # define attribute readers and instance variables |
| 54 | + data_definition_node.attributes.each do |attribute_node, attribute_name| |
| 55 | + name = attribute_name.to_s |
| 56 | + method_pin = Pin::Method.new( |
| 57 | + name: name, |
| 58 | + parameters: [], |
| 59 | + scope: :instance, |
| 60 | + location: get_node_location(attribute_node), |
| 61 | + closure: nspin, |
| 62 | + comments: attribute_comments(attribute_node, attribute_name), |
| 63 | + visibility: :public |
| 64 | + ) |
| 65 | + |
| 66 | + pins.push method_pin |
| 67 | + |
| 68 | + pins.push Pin::InstanceVariable.new(name: "@#{attribute_name}", |
| 69 | + closure: method_pin, |
| 70 | + location: get_node_location(attribute_node), |
| 71 | + comments: attribute_comments(attribute_node, attribute_name)) |
| 72 | + end |
| 73 | + |
| 74 | + process_children region.update(closure: nspin, visibility: :public) |
| 75 | + |
| 76 | + false |
| 77 | + end |
| 78 | + |
| 79 | + private |
| 80 | + |
| 81 | + # @return [DataDefintionNode, nil] |
| 82 | + def data_definition_node |
| 83 | + @data_definition_node ||= if DataDefintionNode.match?(node) |
| 84 | + DataDefintionNode.new(node) |
| 85 | + elsif DataAssignmentNode.match?(node) |
| 86 | + DataAssignmentNode.new(node) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + # @param attribute_node [Parser::AST::Node] |
| 91 | + # @return [String, nil] |
| 92 | + def attribute_comments(attribute_node, attribute_name) |
| 93 | + data_comments = comments_for(attribute_node) |
| 94 | + return if data_comments.nil? || data_comments.empty? |
| 95 | + |
| 96 | + data_comments.split("\n").find do |row| |
| 97 | + row.include?(attribute_name) |
| 98 | + end&.gsub('@param', '@return')&.gsub(attribute_name, '') |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | +end |
0 commit comments