- redef fun content(index, pager) do
- var mdoc = self.mdoc
- if mdoc != null then
- for comment in mdoc.content do pager.add(comment.green)
- end
- pager.add(to_console)
- pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray)
- pager.indent = pager.indent + 1
- var mpropdefs = self.mpropdefs
- var psorter = new MPropDefNameSorter
- index.mainmodule.linearize_mpropdefs(mpropdefs)
- for cat in cats2mpropdefs.keys do
- var defs = cats2mpropdefs[cat].to_a
- psorter.sort(defs)
- if defs.is_empty then continue
- pager.add("")
- pager.add("== {cat}".bold)
- pager.indent = pager.indent + 1
- for mpropdef in defs do
- pager.add("")
- mpropdef.preview(index, pager)
- end
- pager.indent = pager.indent - 1
- end
- pager.indent = pager.indent - 1
- end
-
- # get mpropdefs grouped by categories (vt, init, methods)
- fun cats2mpropdefs: Map[String, Set[MPropDef]] do
- var cats = new ArrayMap[String, Set[MPropDef]]
- cats["virtual types"] = new HashSet[MPropDef]
- cats["constructors"] = new HashSet[MPropDef]
- cats["methods"] = new HashSet[MPropDef]
-
- for mpropdef in mpropdefs do
- if mpropdef isa MAttributeDef then continue
- if mpropdef isa MVirtualTypeDef then cats["virtual types"].add(mpropdef)
- if mpropdef isa MMethodDef then
- if mpropdef.mproperty.is_init then
- cats["constructors"].add(mpropdef)
- else
- cats["methods"].add(mpropdef)
- end
- end
- end
- return cats
- end
-end
-
-redef class MProperty
- super IndexMatch
-
- fun to_console: String do
- if visibility.to_s == "public" then return name.green
- if visibility.to_s == "private" then return name.red
- if visibility.to_s == "protected" then return name.yellow
- return name.bold
- end
-
- redef fun preview(index, pager) do
- intro.preview(index, pager)
- end
-
- redef fun content(index, pager) do
- intro.content(index, pager)
- pager.indent = pager.indent + 1
- var mpropdefs = self.mpropdefs
- index.mainmodule.linearize_mpropdefs(mpropdefs)
- for mpropdef in mpropdefs do
- if mpropdef isa MAttributeDef then continue
- if not mpropdef.is_intro then
- pager.add("")
- mpropdef.preview(index, pager)
- end
- end
- pager.indent = pager.indent - 1
- end
-end
-
-redef class MPropDef
- super IndexMatch
-
- fun to_console: String is abstract
-
- private fun namespace: String do
- return "{mclassdef.namespace}::{mproperty.name}"
- end
-
- redef fun preview(index, pager) do
- var mdoc = self.mdoc
- if mdoc != null then
- pager.add(mdoc.short_comment.green)
- end
- pager.add(to_console)
- pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray)
- end
-
- redef fun content(index, pager) do
- var mdoc = self.mdoc
- if mdoc != null then
- for comment in mdoc.content do pager.add(comment.green)
- end
- pager.add(to_console)
- pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray)
- end
-end
-
-redef class MMethodDef
- redef fun to_console do
- var res = new Buffer
- if not is_intro then res.append("redef ")
- if not mproperty.is_init then res.append("fun ")
- res.append(mproperty.to_console.bold)
- if msignature != null then res.append(msignature.to_console)
- # FIXME: modifiers should be accessible via the model
- #if self isa ADeferredMethPropdef then ret = "{ret} is abstract"
- #if self isa AInternMethPropdef then ret = "{ret} is intern"
- #if self isa AExternMethPropdef then ret = "{ret} is extern"
- return res.to_s
- end
-end
-
-redef class MVirtualTypeDef
- redef fun to_console do
- var res = new Buffer
- res.append("type ")
- res.append(mproperty.to_console.bold)
- res.append(": {bound.to_console}")
- return res.to_s
- end
-end
-
-redef class MAttributeDef
- redef fun to_console do
- var res = new Buffer
- res.append("var ")
- res.append(mproperty.to_console.bold)
- res.append(": {static_mtype.to_console}")
- return res.to_s
- end
-end
-
-redef class MSignature
- redef fun to_console do
- var res = new Buffer
- if not mparameters.is_empty then
- res.append("(")
- for i in [0..mparameters.length[ do
- res.append(mparameters[i].to_console)
- if i < mparameters.length - 1 then res.append(", ")
- end
- res.append(")")
- end
- if return_mtype != null then
- res.append(": {return_mtype.to_console}")
- end
- return res.to_s
- end
-end
-
-redef class MParameter
- fun to_console: String do
- var res = new Buffer
- res.append("{name}: {mtype.to_console}")
- if is_vararg then res.append("...")
- return res.to_s
- end
-end
-
-redef class MType
- fun to_console: String do return self.to_s
-end
-
-redef class MNullableType
- redef fun to_console do return "nullable {mtype.to_console}"
-end
-
-redef class MGenericType
- redef fun to_console do
- var res = new Buffer
- res.append("{mclass.name}[")
- for i in [0..arguments.length[ do
- res.append(arguments[i].to_console)
- if i < arguments.length - 1 then res.append(", ")
- end
- res.append("]")
- return res.to_s
- end
-end
-
-redef class MParameterType
- redef fun to_console do return mclass.intro.parameter_names[rank]
-end
-
-redef class MVirtualType
- redef fun to_console do return mproperty.name
-end
-
-redef class MDoc
- private fun short_comment: String do
- return content.first
- end
-end
-
-redef class AAttrPropdef
- private fun read_accessor: String do
- var ret = "fun "
- #FIXME bug with standard::stream::FDStream::fd
- var name = mreadpropdef.mproperty.name
- if mpropdef.mproperty.visibility.to_s == "public" then ret = "{ret}{name.green}"
- if mpropdef.mproperty.visibility.to_s == "private" then ret = "{ret}{name.red}"
- if mpropdef.mproperty.visibility.to_s == "protected" then ret = "{ret}{name.yellow}"
- ret = "{ret}: {n_type.to_s}"
- if n_kwredef != null then ret = "redef {ret}"
- return ret
- end
-
- private fun write_accessor: String do
- var ret = "fun "
- var name = "{mreadpropdef.mproperty.name}="
- if n_readable != null and n_readable.n_visibility != null then
- if n_readable.n_visibility isa APublicVisibility then ret = "{ret}{name.green}"
- if n_readable.n_visibility isa APrivateVisibility then ret = "{ret}{name.red}"
- if n_readable.n_visibility isa AProtectedVisibility then ret = "{ret}{name.yellow}"
- else
- ret = "{ret}{name.red}"
- end
- ret = "{ret}({mreadpropdef.mproperty.name}: {n_type.to_s})"
- if n_kwredef != null then ret = "redef {ret}"
- return ret
- end
-end
-
-# Redef String class to add a function to color the string
-redef class String
-
- private fun add_escape_char(escapechar: String): String do
- return "{escapechar}{self}\\033[0m"
- end
-
- private fun esc: Char do return 27.ascii
- private fun gray: String do return add_escape_char("{esc}[30m")
- private fun red: String do return add_escape_char("{esc}[31m")
- private fun green: String do return add_escape_char("{esc}[32m")
- private fun yellow: String do return add_escape_char("{esc}[33m")
- private fun blue: String do return add_escape_char("{esc}[34m")
- private fun purple: String do return add_escape_char("{esc}[35m")
- private fun cyan: String do return add_escape_char("{esc}[36m")
- private fun light_gray: String do return add_escape_char("{esc}[37m")
- private fun bold: String do return add_escape_char("{esc}[1m")
- private fun underline: String do return add_escape_char("{esc}[4m")
-
- private fun escape: String
- do
- var b = new Buffer
- for c in self.chars do
- if c == '\n' then
- b.append("\\n")
- else if c == '\0' then
- b.append("\\0")
- else if c == '"' then
- b.append("\\\"")
- else if c == '\\' then
- b.append("\\\\")
- else if c == '`' then
- b.append("'")
- else if c.ascii < 32 then
- b.append("\\{c.ascii.to_base(8, false)}")
- else
- b.add(c)
- end