Skip to content
This repository has been archived by the owner on Dec 29, 2018. It is now read-only.

Commit

Permalink
Rubygems support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomohiro Matsuyama committed Mar 9, 2010
1 parent 4a8bf4e commit 8bc45da
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 89 deletions.
14 changes: 10 additions & 4 deletions bin/rsense
Expand Up @@ -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
Expand All @@ -35,6 +36,8 @@ def process_args
$log = value
when 'debug'
$debug = true
when 'config'
$config = value
end
else
break
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -139,7 +146,6 @@ def server_process
end
end


def start_server
case ARGV[0]
when 'server'
Expand Down
2 changes: 1 addition & 1 deletion build.xml
Expand Up @@ -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" />
Expand Down
7 changes: 7 additions & 0 deletions etc/config.rb
@@ -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)}"
26 changes: 20 additions & 6 deletions etc/rsense.el
Expand Up @@ -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
Expand Down Expand Up @@ -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)))))
Expand Down
45 changes: 42 additions & 3 deletions src/org/cx4a/rsense/CodeAssist.java
Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down
49 changes: 46 additions & 3 deletions src/org/cx4a/rsense/Main.java
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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("\"");
Expand Down

0 comments on commit 8bc45da

Please sign in to comment.