Permalink
Browse files

Add local variables assign/access tracking, method calls. Preparing f…

…or 0.0.1 release
  • Loading branch information...
1 parent c710140 commit 53ad6a445dc67914754c68db723ccdd63ae161ec @dolzenko committed May 17, 2010
View
@@ -1,5 +1,4 @@
tmp/
.idea/
.bundle/
-vendor/
-bin/build_and_install_test_gem
+vendor/
View
@@ -6,8 +6,6 @@ gem "rack"
gem "sinatra"
gem "sinatra_more"
gem "coderay"
-gem "looksee"
-gem "andand"
gem "rdiscount"
group :development do
View
@@ -0,0 +1,31 @@
+$LOAD_PATH.unshift(File.expand_path("../lib", __FILE__))
+
+desc "Run specs (without Rails integration spec - requires rvm, run manually with `spec ./spec/rails_integration_spec.rb')"
+task :spec do
+ sh "spec #{ (Dir["./spec/*_spec.rb"] - ["./spec/rails_integration_spec.rb"]).join(" ") }"
+end
+
+GEM_NAME = "reflexive"
+
+desc "Relese next version of reflexive gem"
+task :release do
+ require "rubygems"
+ require "rubygems/version"
+ require "yaml"
+
+ current_version = YAML.load(`gem specification #{ GEM_NAME } -r`)["version"] || Gem::Version.new("0.0.0")
+ new_version = (current_version.segments[0..-2] + [current_version.segments[-1].succ]).join(".")
+ ENV["GEM_VERSION"] = new_version
+
+ puts "Releasing #{ GEM_NAME } #{ new_version }"
+
+ sh "gem build #{ GEM_NAME }.gemspec --verbose"
+
+ sh "gem push #{ GEM_NAME }-#{ new_version }.gem --verbose"
+
+ sh "gem install #{ GEM_NAME } --version=#{ new_version } --local --verbose"
+
+ File.delete("#{ GEM_NAME }-#{ new_version }.gem")
+end
+
+task :default => :spec
@@ -3,13 +3,9 @@
require "sinatra_more/markup_plugin"
require "coderay"
-require "andand"
require "ostruct"
require "open-uri"
-require "looksee"
-Looksee.styles.each { |k, _| Looksee.styles[k] = "%s" }
-
require "reflexive/faster_open_struct"
require "reflexive/helpers"
require "reflexive/columnizer"
@@ -50,35 +46,28 @@ def root
set :public, self.root + "public"
set :views, self.root + "views"
- def get(path, &block)
- super("/reflexive/#{ path }", &block)
+ def self.action(path, &block)
+ get("/reflexive/#{ path }", &block)
+ end
+
+ def e(message)
+ @message = message
+ erb :error
end
- get "dashboard" do
+ action "dashboard" do
erb :dashboard
end
- get "constant_lookup" do
- name = params[:name]
- scope = eval(params[:scope])
- if scope && name !~ /^::/
- begin
- name_with_scope = "#{ scope.join("::") }::#{ name }"
- klass = Reflexive.constantize(name_with_scope)
- redirect(constant_path(klass.name))
- rescue NameError, ArgumentError
- # For defined top-level module, when looked up from another class:
- # ArgumentError: Object is not missing constant Taggable!
- # from /usr/local/rvm/gems/ruby-1.9.2-head@selfport/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:417:in `load_missing_constant'
- retry if scope.pop
- end
- "Failed to lookup constant: #{ params.inspect }"
+ action "constant_lookup" do
+ if klass = Reflexive.constant_lookup(*params.values_at(:name, :scope))
+ redirect(constant_path(klass.to_s))
else
- redirect(constant_path(name))
+ e "failed to lookup constant `#{ params[:name] }' in scope #{ params[:scope] }"
end
end
- get "files/*" do |path|
+ action "files/*" do |path|
@path = "/" + path
if File.stat(@path).directory?
erb :directories_show
@@ -88,74 +77,91 @@ def get(path, &block)
end
end
- get "constants/:klass/class_methods/:method/definition" do
- find_klass
- @method_name = params[:method]
+ get "/reflexive/load_path_lookup" do
+ path = params[:path]
+ feature = Reflexive.loaded_features_lookup(path) || Reflexive.load_path_lookup(path)
+ if feature
+ redirect(file_path(feature))
+ else
+ e "failed to find feature: #{ path }"
+ end
+ end
+
+ get %r</reflexive/constants/([^/&#]+)/class_methods/([^/&#]+)/definition> do |klass, method|
+ find_klass(klass)
+ @method_name = method
@path, @line = @klass.method(@method_name).source_location
@source = highlight_file(@path, :highlight_lines => [@line])
erb :methods_definition
end
- get "constants/:klass/instance_methods/:method/definition" do
- find_klass
- @method_name = params[:method]
+ get %r</reflexive/constants/([^/&#]+)/instance_methods/([^/&#]+)/definition> do |klass, method|
+ find_klass(klass)
+ @method_name = method
@path, @line = @klass.instance_method(@method_name).source_location
@source = highlight_file(@path, :highlight_lines => [@line])
erb :methods_definition
end
- get "/reflexive/constants/:klass/methods/:method/apidock" do
- find_klass
- @method_name = params[:method]
+ get %r</reflexive/constants/([^/&#]+)/methods/([^/&#]+)/apidock> do |klass, method|
+ find_klass(klass)
+ @method_name = method
erb :methods_apidock
end
- get "constants/:klass/class_methods/:method" do
- find_klass
- if @klass.method(params[:method]).source_location
- redirect(class_method_definition_path(params[:klass], params[:method]) +
- "#highlighted")
- else
- redirect(method_documentation_path(params[:klass], params[:method]))
+ get %r</reflexive/constants/([^/&#]+)/class_methods/([^/&#]+)> do |klass, method|
+ find_klass(klass)
+ begin
+ if @klass.method(method).source_location
+ redirect(class_method_definition_path(klass, method) +
+ "#highlighted")
+ else
+ redirect(method_documentation_path(klass, method))
+ end
+ rescue NameError
+ e "failed to find `#{ method }' class method for #{ klass }"
end
end
- get "constants/:klass/instance_methods/:method" do
- find_klass
- if @klass.instance_method(params[:method]).source_location
- redirect(instance_method_definition_path(params[:klass], params[:method]) +
- "#highlighted")
- else
- redirect(method_documentation_path(params[:klass], params[:method]))
+ get %r</reflexive/constants/([^/&#]+)/instance_methods/([^/&#]+)> do |klass, method|
+ find_klass(klass)
+ begin
+ if @klass.instance_method(method).source_location
+ redirect(instance_method_definition_path(klass, method) +
+ "#highlighted")
+ else
+ redirect(method_documentation_path(klass, method))
+ end
+ rescue NameError
+ e "failed to find `#{ method }' instance method for #{ klass }"
end
end
- get "constants/:klass" do
- # r Class.constants
- find_klass
- @methods = Reflexive::Methods.new(@klass)
- # @methods = Faster::OpenStruct.new(:klass => Faster::OpenStruct.new,
- # :instance => Faster::OpenStruct.new)
- #
- # %w(public protected private).each do |visibility|
- # if (methods = @klass.send("#{ visibility }_methods").sort).present?
- # @methods.klass.send("#{ visibility }=", methods)
- # end
- #
- # if (methods = @klass.send("#{ visibility }_instance_methods").sort).present?
- # @methods.instance.send("#{ visibility }=", methods)
- # end
- # end
- #
+ get %r</reflexive/constants/([^/&#]+)> do |klass|
+ find_klass(klass)
+
+ exclude_trite = ![ BasicObject, Object ].include?(@klass)
+ @methods = Reflexive::Methods.new(@klass, :exclude_trite => exclude_trite)
+
+ ancestors_without_self_and_super = @klass.ancestors[2..-1] || []
+ class_ancestors = ancestors_without_self_and_super.select { |ancestor| ancestor.class == Class }
+ @class_ancestors = class_ancestors if class_ancestors.size > 0
+
+ if @klass.respond_to?(:superclass) &&
+ @klass.superclass != Object &&
+ @klass.superclass != nil
+ @superclass = @klass.superclass
+ end
+
erb :constants_show
end
protected
error(404) { @app.call(env) if @app }
- def find_klass
- @klass = Reflexive.constantize(params[:klass]) if params[:klass]
+ def find_klass(klass = params[:klass])
+ @klass = Reflexive.constantize(klass) if klass
end
end
end
@@ -6,17 +6,32 @@
module Reflexive
class CodeRayHtmlEncoder < ::CodeRay::Encoders::HTML
- require "cgi" unless defined?(CGI) && defined?(CGI::escape)
-
include RoutingHelpers
def token(text, type = :plain, tags = {})
- if type == :constant
- @out << "<a href='#{ constant_lookup_path(text, tags[:scope]) }'>"
+ if constant_access = tags[:constant_access]
+ name, scope = constant_access.values_at(:name, :scope)
+ @out << "<a href='#{ constant_lookup_path(name, scope) }'>"
super(text, type)
@out << "</a>"
elsif type == :meta_scope
# pass
+ elsif type == :content && tags[:load_path]
+ @out << "<a href='#{ load_path_lookup_path(text) }'>"
+ super(text, type)
+ @out << "</a>"
+ elsif method_call = tags[:method_call]
+ @out << "<a href='#{ method_call_path(method_call) }'>"
+ super(text, type)
+ @out << "</a>"
+ elsif local_variable_assignment = tags[:local_variable_assignment]
+ @out << "<span id='lv:#{ local_variable_assignment }'>"
+ super(text, type)
+ @out << "</span>"
+ elsif local_variable_access = tags[:local_variable_access]
+ @out << "<a href='#lv:#{ local_variable_access }' class='lva'>"
+ super(text, type)
+ @out << "</a>"
else
super(text, type) rescue raise([text, type].inspect)
end
@@ -49,11 +49,19 @@ def coderay_tokens(scanner_events)
in_backtick = false
in_symbol = false
in_embexpr_nesting = 0
- scanner_events.each do |token_val, event|
- if event == :meta_scope
- @coderay_tokens << [token_val, event]
- next
- end
+ scanner_events.each do |scanner_event|
+ token_val, event, tags =
+ ReflexiveRipper.destruct_scanner_event(scanner_event)
+
+ # if event == :meta_scope
+ # @coderay_tokens << [token_val, event]
+ # next
+ # end
+ #
+ # if token_val == :method_call
+ # @coderay_tokens << [token_val, event, tags]
+ # next
+ # end
ripper_token = SCANNER_EVENT_TO_CODERAY_TOKEN[event.to_sym] || event.to_sym
if in_backtick && event == :lparen
@@ -139,15 +147,19 @@ def coderay_tokens(scanner_events)
end
else
if @coderay_tokens.size > 0 && @coderay_tokens[-1][1] == ripper_token
- @coderay_tokens[-1][0] << token_val
+ @coderay_tokens.last[0] << token_val # same consequent tokens get squeezed
else
@coderay_tokens << [token_val, ripper_token]
+ @coderay_tokens.last << tags if tags
end
end
end
-
- inject_constant_tags(squeeze_constants(@coderay_tokens))
+ tokens = @coderay_tokens
+ # tokens = squeeze_constants(tokens)
+ # tokens = inject_constant_tags(tokens)
+ tokens = inject_load_path_tags(tokens)
+ tokens
end
def squeeze_constants(coderay_tokens)
@@ -175,6 +187,14 @@ def inject_constant_tags(coderay_tokens)
coderay_tokens.each do |token|
if token[1] == :constant
token << { :scope => scope }
+ elsif token[0] == :method_call &&
+ token[1] == :open
+ begin
+ token[2][:scope] = scope
+ rescue Exception
+ r(token)
+ end
+
elsif token[1] == :meta_scope
scope = token[0]
scope = nil if scope.empty?
@@ -183,6 +203,27 @@ def inject_constant_tags(coderay_tokens)
coderay_tokens
end
+ def inject_load_path_tags(coderay_tokens)
+ state = nil
+ coderay_tokens.each do |token|
+ value, type = token
+ if state == nil && type == :ident && value =~ /\A(require|load)\z/
+ state = :ident
+ elsif state == :ident && [ :operator, :space ].include?(type)
+ # skip
+ elsif state == :ident && value == :open && type == :string
+ state = :open_string
+ elsif state == :open_string && type == :delimiter
+ # skip
+ elsif state == :open_string && type == :content
+ token << { :load_path => true }
+ else
+ state = nil
+ end
+ end
+ coderay_tokens
+ end
+
def scan_tokens(tokens, options)
parser = ReflexiveRipper.new(code)
parser.parse
Oops, something went wrong.

0 comments on commit 53ad6a4

Please sign in to comment.