Class: Fast::Node

Inherits:
Astrolabe::Node
  • Object
show all
Defined in:
lib/fast.rb,
lib/fast/git.rb

Overview

This is not required by default, so to use it, you should require it first.

@example
  require 'fast/git'
  Fast.ast_from_file('lib/fast.rb').git_log.first.author.name # => "Jonatas Davi Paganini"

Direct Known Subclasses

SQL::Node

Instance Method Summary collapse

Instance Method Details

#authorString

Returns with the first element from #blame_authors.

Returns:

  • (String)

    with the first element from #blame_authors



100
101
102
# File 'lib/fast.rb', line 100

def author
  blame_authors.first
end

#blame_authorsArray<String>

Returns with authors from the current expression range.

Returns:

  • (Array<String>)

    with authors from the current expression range



93
94
95
96
97
# File 'lib/fast.rb', line 93

def blame_authors
  `git blame -L #{expression.first_line},#{expression.last_line} #{buffer_name}`.lines.map do |line|
    line.split('(')[1].split(/\d+/).first.strip
  end
end

#buffer_nameString

Returns with path of the file or simply buffer name.

Returns:

  • (String)

    with path of the file or simply buffer name.



73
74
75
# File 'lib/fast.rb', line 73

def buffer_name
  expression.source_buffer.name
end

#capture(pattern, *args) ⇒ Array<Fast::Node>

Captures elements from search recursively

Parameters:

  • pattern (String)
  • *args (Array)

    extra arguments to interpolate in the pattern.

Returns:

  • (Array<Fast::Node>)

    ] with files and results



116
117
118
# File 'lib/fast.rb', line 116

def capture(pattern, *args)
  Fast.capture(pattern, self, *args)
end

#expressionParser::Source::Range

Returns from the expression.

Returns:

  • (Parser::Source::Range)

    from the expression



78
79
80
# File 'lib/fast.rb', line 78

def expression
  location.expression
end

#fileObject



58
59
60
# File 'lib/fast/git.rb', line 58

def file
  buffer_name.gsub("#{Dir.pwd}/", '')
end

#from_file?Boolean

Returns true if a file exists with the #buffer_name.

Returns:

  • (Boolean)

    true if a file exists with the #buffer_name



88
89
90
# File 'lib/fast.rb', line 88

def from_file?
  File.exist?(buffer_name)
end

#gitGit::Base

Returns from current directory.

Returns:

  • (Git::Base)

    from current directory



13
14
15
16
# File 'lib/fast/git.rb', line 13

def git
  require 'git' unless defined? Git
  Git.open('.')
end

#git_blobGit::Object::Blob

Returns from current #buffer_name.

Returns:

  • (Git::Object::Blob)

    from current #buffer_name



19
20
21
22
23
# File 'lib/fast/git.rb', line 19

def git_blob
  return unless from_file?

  git.gblob(buffer_name)
end

#git_logGit::Log

Returns from the current #git_blob buffer-name.

Returns:

  • (Git::Log)

    from the current #git_blob buffer-name



27
28
29
# File 'lib/fast/git.rb', line 27

def git_log
  git_blob.log
end

#last_commitGit::Object::Commit

Returns:

  • (Git::Object::Commit)


32
33
34
# File 'lib/fast/git.rb', line 32

def last_commit
  git_log.first
end

#line_rangeObject

Returns:



63
64
65
# File 'lib/fast/git.rb', line 63

def line_range
  lines.map { |l| "L#{l}" }.join('-')
end

#linesArray

Returns with lines range.

Returns:

  • (Array)

    with lines range



68
69
70
71
72
73
# File 'lib/fast/git.rb', line 68

def lines
  exp = loc.expression
  first_line = exp.first_line
  last_line = exp.last_line
  [first_line, last_line].uniq
end

#lines_of_codeInteger

Returns lines of code from current block.

Returns:

  • (Integer)

    lines of code from current block



76
77
78
# File 'lib/fast/git.rb', line 76

def lines_of_code
  lines.last - lines.first + 1
end

Returns with formatted repositorym link.

Returns:

  • (String)

    with formatted repositorym link



92
93
94
# File 'lib/fast/git.rb', line 92

def link
  "#{project_url}/blob/master/#{buffer_name}##{line_range}"
end

Returns a markdown link with #md_link_description and #link.

Returns:

  • (String)

    a markdown link with #md_link_description and #link



81
82
83
# File 'lib/fast/git.rb', line 81

def md_link(text = md_link_description)
  "[#{text}](#{link})"
end

able to create a markdown link without parens.

Returns:

  • (String)

    with the source cutting arguments from method calls to be



87
88
89
# File 'lib/fast/git.rb', line 87

def md_link_description
  source[/([^\r\(]+)\(/, 1] || source
end

Returns with permanent link to the actual commit.

Returns:

  • (String)

    with permanent link to the actual commit



97
98
99
# File 'lib/fast/git.rb', line 97

def permalink
  "#{project_url}/blob/#{sha}/#{buffer_name}##{line_range}"
end

#project_urlString

Given #remote_url is “[email protected]:namespace/project.git” Or #remote_url is “github.com/namespace/project.git

Returns:



49
50
51
52
53
54
55
56
# File 'lib/fast/git.rb', line 49

def project_url
  return remote_url.gsub(/\.git$/, '') if remote_url.start_with?('https')

  remote_url
    .gsub('git@', 'https://')
    .gsub(/:(\w)/, '/\\1')
    .gsub(/\.git$/, '')
end

#remote_urlString

Returns with remote URL.

Returns:

  • (String)

    with remote URL



42
43
44
# File 'lib/fast/git.rb', line 42

def remote_url
  git.remote.url
end

#search(pattern, *args) ⇒ Array<Fast::Node>

Search recursively into a node and its children using a pattern.

Parameters:

  • pattern (String)
  • *args (Array)

    extra arguments to interpolate in the pattern.

Returns:

  • (Array<Fast::Node>)

    ] with files and results



108
109
110
# File 'lib/fast.rb', line 108

def search(pattern, *args)
  Fast.search(pattern, self, *args)
end

#shaString

Returns with last commit SHA.

Returns:

  • (String)

    with last commit SHA



37
38
39
# File 'lib/fast/git.rb', line 37

def sha
  last_commit.sha
end

#sourceString

Returns with the content of the #expression.

Returns:

  • (String)

    with the content of the #expression



83
84
85
# File 'lib/fast.rb', line 83

def source
  expression.source
end