Permalink
Browse files

Rubygems support.

  • Loading branch information...
1 parent 4a8bf4e commit 8bc45dab578fe7de6d80059e3c40745e166b7bf0 Tomohiro Matsuyama committed Mar 9, 2010
Showing with 247 additions and 89 deletions.
  1. +10 −4 bin/rsense
  2. +1 −1 build.xml
  3. +7 −0 etc/config.rb
  4. +20 −6 etc/rsense.el
  5. +42 −3 src/org/cx4a/rsense/CodeAssist.java
  6. +46 −3 src/org/cx4a/rsense/Main.java
  7. +105 −58 src/org/cx4a/rsense/Options.java
  8. +16 −14 src/org/cx4a/rsense/Project.java
View
@@ -9,6 +9,7 @@ $classpath = ENV['RSENSE_CLASSPATH']
$pid_file = ENV['RSENSE_PID_FILE']
$log = ENV['RSENSE_LOG']
$debug = ENV['RSENSE_DEBUG']
+$config = ENV['RSENSE_CONFIG']
$end_mark = 'END'
def process_args
@@ -35,6 +36,8 @@ def process_args
$log = value
when 'debug'
$debug = true
+ when 'config'
+ $config = value
end
else
break
@@ -61,7 +64,11 @@ def process_args
end
end
- $classpath = '.' + ['', 'rsense.jar', 'antlr-runtime-3.2.jar', 'jruby.jar'].join("#{File::PATH_SEPARATOR}#{$rsense_home}/lib/") unless $classpath
+ sep = File::SEPARATOR
+ psep = File::PATH_SEPARATOR
+ $classpath = '.' + ['', 'rsense.jar', 'antlr-runtime-3.2.jar', 'jruby.jar'].join("#{psep}#{$rsense_home}#{sep}lib#{sep}") unless $classpath
+
+ $config = "#{ENV['HOME']}#{sep}.rsense" unless $config
ARGV << 'help' if ARGV.empty?
end
@@ -99,7 +106,7 @@ end
def server_process
begin
- options = "--rsense-home=#{$rsense_home} --no-prompt --end-mark=#{$end_mark} #{'--debug' if $debug} #{'--log=' + $log if $log}"
+ options = "--home=#{$rsense_home} --no-prompt --end-mark=#{$end_mark} #{'--debug' if $debug} #{'--log=' + $log if $log} --config=#{$config}"
io = IO.popen("java -cp #{$classpath} org.cx4a.rsense.Main script #{options}", 'r+')
open_server do |serv|
begin
@@ -113,7 +120,7 @@ def server_process
if cmd =~ /^(exit|quit)/
sock.puts('END')
sock.close
- exit
+ return
end
io.puts(cmd)
sock.close_read
@@ -139,7 +146,6 @@ def server_process
end
end
-
def start_server
case ARGV[0]
when 'server'
View
@@ -21,7 +21,7 @@
</target>
<target name="build" depends="init">
- <javac fork="true" source="1.6" deprecation="true"
+ <javac fork="true" debug="true" source="1.6" deprecation="true"
srcdir="src" destdir="build"
classpathref="build.class.path">
<compilerarg value="-Xlint:all" />
View
@@ -0,0 +1,7 @@
+#!/usr/bin/env ruby
+
+require 'rubygems'
+
+puts "home = #{File.expand_path(File.dirname(File.dirname($0)))}"
+puts "load-path = #{$:.join(File::PATH_SEPARATOR)}"
+puts "gem-path = #{Gem.path.join(File::PATH_SEPARATOR)}"
View
@@ -89,17 +89,23 @@ Nil means proper socket will be selected.")
"--")
args)))
+(defun rsense-command-1 (command no-output)
+ (apply 'call-process
+ (rsense-interpreter)
+ nil (not no-output) nil
+ (cons (rsense-program)
+ (apply 'rsense-args
+ (append command '("--format=emacs"))))))
+
(defun rsense-command (&rest command)
(car-safe
(read-from-string
(with-output-to-string
(with-current-buffer standard-output
- (apply 'call-process
- (rsense-interpreter)
- nil t nil
- (cons (rsense-program)
- (apply 'rsense-args
- (append command '("--format=emacs"))))))))))
+ (rsense-command-1 command nil))))))
+
+(defun rsense-command-no-output (&rest command)
+ (rsense-command-1 command t))
(defun rsense-buffer-command (buffer offset command &optional remove-until prefix)
(unless rsense-temp-file
@@ -136,6 +142,14 @@ Nil means proper socket will be selected.")
(interactive)
(message "%s" (rsense-command "version")))
+(defun rsense-version ()
+ (interactive)
+ (message "%s" (rsense-command "version")))
+
+(defun rsense-clear ()
+ (interactive)
+ (rsense-command-no-output "clear"))
+
(defun rsense-type-help ()
(interactive)
(let ((result (assoc-default 'type (rsense-type-inference (current-buffer) (point)))))
@@ -225,6 +225,7 @@ private Project newProjectFromConfig(File config, Options options) {
File path = config.getParentFile();
Project project = new Project(path.getName(), path);
project.setLoadPath(options.getLoadPath());
+ project.setGemPath(options.getGemPath());
return project;
}
@@ -233,10 +234,10 @@ public LoadResult load(Project project, File file, String encoding) {
}
private LoadResult load(Project project, File file, String encoding, boolean prepare) {
- if (project.isLoaded(file)) {
+ if (project.isLoaded(file.getPath())) {
return LoadResult.alreadyLoaded();
}
- project.setLoaded(file);
+ project.setLoaded(file.getPath());
try {
InputStream in = new FileInputStream(file);
@@ -275,8 +276,18 @@ public LoadResult require(Project project, String feature, String encoding) {
}
private LoadResult require(Project project, String feature, String encoding, int loadPathLevel) {
+ if (project.isLoaded(feature)) {
+ return LoadResult.alreadyLoaded();
+ }
+ project.setLoaded(feature);
+
+ if (File.pathSeparator.equals(";")) { // Windows?
+ feature = feature.replace('/', '\\');
+ }
+
List<String> loadPath = project.getLoadPath();
- for (int i = loadPathLevel; i < loadPath.size(); i++) {
+ int loadPathLen = loadPath.size();
+ for (int i = loadPathLevel; i < loadPathLen; i++) {
String pathElement = loadPath.get(i);
int oldLoadPathLevel = context.loadPathLevel;
context.loadPathLevel = i;
@@ -292,6 +303,33 @@ private LoadResult require(Project project, String feature, String encoding, int
context.loadPathLevel = oldLoadPathLevel;
}
}
+
+ List<String> gemPath = project.getGemPath();
+ int gemPathLen = gemPath.size();
+ String sep = File.separator;
+ for (int i = 0; i < gemPathLen; i++) {
+ String pathElement = gemPath.get(i);
+ int oldLoadPathLevel = context.loadPathLevel;
+ context.loadPathLevel = i + loadPathLen;
+ String oldFeature = context.feature;
+ context.feature = feature;
+ try {
+ File gemsDir = new File(pathElement, "gems");
+ String[] gems = gemsDir.list();
+ if (gems != null) {
+ for (String gem : gems) {
+ File file = new File(gemsDir + sep + gem + sep + "lib" + sep + feature + ".rb");
+ if (file.exists()) {
+ return load(project, file, encoding, false);
+ }
+ }
+ }
+ } finally {
+ context.feature = oldFeature;
+ context.loadPathLevel = oldLoadPathLevel;
+ }
+ }
+
Logger.warn("cannot require: %s", feature);
return LoadResult.failWithNotFound();
}
@@ -377,6 +415,7 @@ public void clear() {
this.projects = new HashMap<File, Project>();
this.sandbox = new Project("(sandbox)", null);
this.sandbox.setLoadPath(options.getLoadPath());
+ this.sandbox.setGemPath(options.getGemPath());
}
private void prepare(Project project) {
@@ -107,21 +107,26 @@ private void usage() {
+ " --prompt= - Prompt string in interactive shell mode\n"
+ " --no-prompt - Do not show prompt\n"
+ "\n"
+ + " environment - Print environment.\n"
+ " help - Print this help.\n"
+ "\n"
+ " version - Print version information.\n"
+ "\n"
+ "script-command:\n"
- + " exit/quit - Exit script.\n"
+ + " exit\n"
+ + " quit - Exit script.\n"
+ "\n"
+ " clear - Clear current environment.\n"
+ "\n"
+ "common-options:\n"
- + " --rsense-home= - Specify RSense home directory\n"
+ + " --home= - Specify RSense home directory\n"
+ " --debug - Print debug messages\n"
+ " --log= - Log file to output (default stderr)\n"
+ " --format= - Output format (plain, emacs)\n"
+ " --encoding= - Input encoding\n"
+ + " --load-path= - Load path string (: or ; separated)\n"
+ + " --gem-path= - Gem path string (: or ; separated)\n"
+ + " --config= - Config file\n"
+ "\n"
+ "test-options:\n"
+ " --test= - Specify fixture name\n"
@@ -151,12 +156,33 @@ private Options parseOptions(String[] args, int offset) {
if (arg.startsWith("--")) {
String[] lr = arg.substring(2).split("=");
if (lr.length >= 1) {
- options.put(lr[0], lr.length >= 2 ? lr[1] : "");
+ options.addOption(lr[0], lr.length >= 2 ? lr[1] : null);
}
} else {
options.addRestArg(arg);
}
}
+ try {
+ // load config
+ String config = options.getConfig();
+ if (config != null && new File(config).exists()) {
+ InputStream in = new FileInputStream(config);
+ try {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+ String line;
+ while ((line = reader.readLine()) != null) {
+ String[] lr = line.split("\\s*=\\s*", 2);
+ if (lr.length >= 1) {
+ options.addOption(lr[0], lr.length >= 2 ? lr[1] : null);
+ }
+ }
+ } finally {
+ in.close();
+ }
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
return options;
}
@@ -251,6 +277,8 @@ private void command(String command, Options options) {
script(options);
} else if (command.equals("clear")) {
commandClear(options);
+ } else if (command.equals("environment")) {
+ commandEnvironment(options);
} else if (command.equals("help")) {
commandHelp(options);
} else if (command.equals("version")) {
@@ -401,6 +429,21 @@ private void commandClear(Options options) {
codeAssist.clear();
}
+ private void commandEnvironment(Options options) {
+ out.println("version: " + versionString());
+ out.println("home: " + options.getRsenseHome());
+ out.println("debug: " + (options.isDebug() ? "yes" : "no"));
+ out.println("log: " + (options.getLog() != null ? options.getLog() : ""));
+ out.println("load-path:");
+ for (String path : options.getLoadPath()) {
+ out.println(" - " + path);
+ }
+ out.println("gem-path:");
+ for (String path : options.getLoadPath()) {
+ out.println(" - " + path);
+ }
+ }
+
private void commandHelp(Options options) {
if (options.isEmacsFormat()) {
out.print("\"");
Oops, something went wrong.

0 comments on commit 8bc45da

Please sign in to comment.