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

Commit

Permalink
Added some more utility methods to SourceOpContext
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed Jun 21, 2016
1 parent 60ef5bf commit 9d2f4cc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
package melnorme.lang.tooling.toolchain.ops;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;
import static melnorme.utilbox.core.CoreUtil.areEqual;

import java.util.Optional;

import melnorme.lang.tooling.ast.SourceRange;
import melnorme.lang.tooling.common.SourceLineColumnRange;
import melnorme.lang.tooling.parser.SourceLinesInfo;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.FileUtil;
import melnorme.utilbox.misc.Location;
import melnorme.utilbox.misc.StringUtil;

/**
* Parameters for a source operation. This data from this class is immutable.
Expand Down Expand Up @@ -94,4 +98,24 @@ public int getInvocationColumn_0() throws CommonException {
return getSourceLinesInfo().getColumnForOffset(getOffset());
}

/* ----------------- ----------------- */

public String getSourceFor(Location location) throws CommonException {
if(this.fileLocation.isPresent()) {
if(areEqual(location, fileLocation.get())) {
return source;
}
}
// TODO: we might need to do auto-detect of encoding.
return FileUtil.readFileContents(location, StringUtil.UTF8);
}

public int getOffsetFor(SourceLocation findDefResult) throws CommonException {
String fileContents = getSourceFor(findDefResult.getFileLocation());
SourceLinesInfo linesInfo = new SourceLinesInfo(fileContents);

SourceLineColumnRange sourceLCRange = findDefResult.getSourceRange();
return linesInfo.getOffsetForLine(sourceLCRange.getValidLineIndex()) + sourceLCRange.getValidColumnIndex();
}

}
9 changes: 4 additions & 5 deletions plugin_tooling/src-util/melnorme/utilbox/misc/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.EnumSet;

import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.core.fntypes.Getter;

/**
* Miscellaneous file utilities.
Expand Down Expand Up @@ -69,15 +68,15 @@ public static String readStringFromFile(Path file, Charset charset) throws IOExc
return readBytesFromFile(file.toFile()).toString(charset);
}

public static String readStringFromFile(Location loc, Charset charset, Getter<String, RuntimeException> errorMsg)
throws CommonException {
public static String readFileContents(Location location, Charset charset) throws CommonException {
try {
return readStringFromFile(loc.toFile(), charset);
return readStringFromFile(location.toPath(), charset);
} catch(IOException e) {
throw new CommonException(errorMsg.get(), e);
throw new CommonException(e.getMessage(), e.getCause());
}
}

/* ----------------- write ----------------- */

/** Write the given array of bytes to given file */
public static void writeBytesToFile(File file, byte[] bytes) throws IOException {
Expand Down

0 comments on commit 9d2f4cc

Please sign in to comment.