فهرست منبع

[display] add haxe.display.Position
[macro] add PositionTools.toRange

closes #6599

Simon Krajewski 8 سال پیش
والد
کامیت
689ce5250f
4فایلهای تغییر یافته به همراه104 افزوده شده و 0 حذف شده
  1. 21 0
      src/macro/macroApi.ml
  2. 70 0
      std/haxe/display/Position.hx
  3. 1 0
      std/haxe/macro/Context.hx
  4. 12 0
      std/haxe/macro/PositionTools.hx

+ 21 - 0
src/macro/macroApi.ml

@@ -1903,6 +1903,27 @@ let macro_api ccom get_api =
 			) (decode_array a);
 			vnull
 		);
+		"position_to_range", vfun1 (fun p ->
+			let p = decode_pos p in
+			let l1,c1,l2,c2 = Lexer.get_pos_coords p in
+			let make_pos line character =
+				encode_obj O__Const [
+					"line",vint line;
+					"character",vint character;
+				]
+			in
+			let pos_start = make_pos l1 c1 in
+			let pos_end = make_pos l2 c2 in
+			let range = encode_obj O__Const [
+				"start",pos_start;
+				"end",pos_end;
+			] in
+			let location = encode_obj O__Const [
+				"uri",encode_string p.Globals.pfile;
+				"range",range
+			] in
+			location
+		);
 	]
 
 

+ 70 - 0
std/haxe/display/Position.hx

@@ -0,0 +1,70 @@
+/*
+ * Copyright (C)2005-2017 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package haxe.display;
+
+abstract DocumentUri(String) {
+    public inline function new(uri:String) {
+        this = uri;
+    }
+
+    public inline function toString() {
+        return this;
+    }
+}
+
+/**
+    Position in a text document expressed as zero-based line and character offset.
+**/
+typedef Position = {
+    /**
+        Line position in a document (zero-based).
+    **/
+    var line:Int;
+
+    /**
+        Character offset on a line in a document (zero-based).
+    **/
+    var character:Int;
+}
+
+/**
+    A range in a text document expressed as (zero-based) start and end positions.
+**/
+typedef Range = {
+    /**
+        The range's start position
+    **/
+    var start:Position;
+
+    /**
+        The range's end position
+    **/
+    var end:Position;
+}
+
+/**
+    Represents a location inside a resource, such as a line inside a text file.
+**/
+typedef Location = {
+    var uri:DocumentUri;
+    var range:Range;
+}

+ 1 - 0
std/haxe/macro/Context.hx

@@ -606,6 +606,7 @@ class Context {
 	@:allow(haxe.macro.TypeTools)
 	@:allow(haxe.macro.MacroStringTools)
 	@:allow(haxe.macro.TypedExprTools)
+	@:allow(haxe.macro.PositionTools)
 	static function load( f, nargs ) : Dynamic {
 		#if neko
 		return neko.Lib.load("macro", f, nargs);

+ 12 - 0
std/haxe/macro/PositionTools.hx

@@ -21,6 +21,7 @@
  */
  package haxe.macro;
 
+import haxe.display.Position;
 import haxe.macro.Expr;
 
 class PositionTools {
@@ -59,4 +60,15 @@ class PositionTools {
 		#end
 	}
 
+	#if macro
+	/**
+		Converts a `haxe.macro.Position` to a `haxe.display.Range`.
+
+		This operation requires the source file the be known to the Haxe lexer in order
+		to determine line breaks. It is thus only available in macro context.
+	**/
+	public static function toRange(p:Position):Range {
+		return Context.load("position_to_range", 1)(p);
+	}
+	#end
 }