Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Exposing lexical information in TS frontend app #22
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniogarrote committed Apr 10, 2017
1 parent 821b7cb commit 70e8da5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 14 deletions.
25 changes: 14 additions & 11 deletions api-modeller-web/src/main/model_proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,20 @@ export class ModelProxy {
}
}

elementLexicalInfo(id: string): LexicalInfo {
elementLexicalInfo(id: string): LexicalInfo | undefined {
console.log("*** Looking for lexical information about " + id);
const res = apiFramework.lexical_info_for_unit(this.raw, id);

return new LexicalInfo(
parseInt(res["start-line"]),
parseInt(res["start-column"]),
parseInt(res["start-index"]),
parseInt(res["end-line"]),
parseInt(res["end-column"]),
parseInt(res["end-index"])
);
const res = apiFramework.lexical_info_for_unit(this.raw, "raml", id);
if (res != null) {
return new LexicalInfo(
parseInt(res["start-line"]),
parseInt(res["start-column"]),
parseInt(res["start-index"]),
parseInt(res["end-line"]),
parseInt(res["end-column"]),
parseInt(res["end-index"])
);
} else {
return undefined;
}
}
}
2 changes: 2 additions & 0 deletions api-modeller-web/src/view_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ export class ViewModel {
}
}
this.focusedId(unit.id);
debugger;
this.model.elementLexicalInfo(unit.id);
this.domainUnits({});
this.resetDomainUnits();
this.resetDiagram();
Expand Down
21 changes: 18 additions & 3 deletions src/api_modelling_framework/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
(^:export update-reference-model [this location syntax-type text cb] "Updates a model for a reference model")
(^:export references [this] "Returns a list of all the files referenced by this model")
(^:export find-element [this level id] "Finds a domain element in the model data, returning the element wrapped in a fragment")
(^:export raw [this] "Returns the raw text for the model"))
(^:export raw [this] "Returns the raw text for the model")
(^:export lexical-info-for-unit [model syntax unit-id] "Finds lexical information for a particular unit for a particular syntax (\"raml\", \"openapi\")"))

(defprotocol Parser
(^:export parse-file
Expand Down Expand Up @@ -260,7 +261,8 @@
"open-api" (parse-string (OpenAPIParser.) location text {} (fn [e r] (go (>! ch r)))))
ch))]

(let [domain-cache (atom nil)]
(let [domain-cache (atom nil)
lexical-cache-raml (atom {})]
(reify Model
(location [_] (document/location res))

Expand All @@ -275,7 +277,7 @@
(let []
(cb nil (update-reference-model* res parsed-unit)))))
(cb {:err (new #?(:clj Exception :cljs js/Error)
(str "Unsupported spec " syntax-type " only 'ram' and 'openapi' supported"))}
(str "Unsupported spec " syntax-type " only 'raml' and 'openapi' supported"))}
nil))))

(domain-model [_]
Expand Down Expand Up @@ -314,4 +316,17 @@
(map :location))
platform/<-clj))

(lexical-info-for-unit [this syntax unit-id]
(let [cache lexical-cache-raml]
(platform/<-clj
(if-let [lexical-info (get @cache unit-id)]
lexical-info
(let [element (find-element* res unit-id)
lexical-info (:lexical element)]
(if (some? lexical-info)
(do
(swap! cache (fn [acc] (assoc acc unit-id lexical-info)))
lexical-info)
nil))))))

(raw [this] (:raw res)))))))
22 changes: 22 additions & 0 deletions test/api_modelling_framework/core_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(ns api-modelling-framework.core-test
#?(:cljs (:require-macros [cljs.test :refer [deftest is async]]
[cljs.core.async.macros :refer [go]]))
(:require #?(:clj [clojure.test :refer :all])
#?(:clj [clojure.core.async :refer [go <! >! chan]])
#?(:clj [api-modelling-framework.platform :refer [async]])
#?(:clj [clojure.test :refer [deftest is]])
[api-modelling-framework.utils-test :refer [cb->chan error?]]
#?(:cljs [cljs.core.async :refer [<! >! chan]])
[api-modelling-framework.core :as core]))

(deftest lexical-info-test
(async done
(go
(let [parser (core/->RAMLParser)
generator (core/->OpenAPIGenerator)
model (<! (cb->chan (partial core/parse-file parser "resources/world-music-api/wip.raml")))
id #?(:cljs "file://resources/world-music-api/wip.raml#/api-documentation/end-points/0/post/body"
:clj "resources/world-music-api/wip.raml#/api-documentation/end-points/0/post/body")
info(core/lexical-info-for-unit model "raml" id)]
(is (some? info))
(done)))))
2 changes: 2 additions & 0 deletions test/api_modelling_framework/runner.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
[api-modelling-framework.generators.domain.raml-test]
[api-modelling-framework.generators.document.openapi-test]
[api-modelling-framework.model.document-test]
[api-modelling-framework.core-test]
))

(doo-tests 'api-modelling-framework.tck
Expand All @@ -34,4 +35,5 @@
'api-modelling-framework.generators.domain.raml-test
'api-modelling-framework.generators.document.raml-test
'api-modelling-framework.generators.document.openapi-test
'api-modelling-framework.core-test
)

0 comments on commit 70e8da5

Please sign in to comment.