Commit 59e527d4 authored by Lucie Dvorakova's avatar Lucie Dvorakova Committed by Christian Hergert

documentation-card: card showing relevant information about a library function

https://bugzilla.gnome.org/show_bug.cgi?id=785854
parent 9ef665eb
/* ide-documentation-info.c
*
* Copyright (C) 2017 Lucie Charvat <luci.charvat@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define G_LOG_DOMAIN "ide-documentation-info"
#include "ide-documentation-info.h"
#include "ide-documentation-proposal.h"
struct _IdeDocumentationInfo
{
GObject parent_instance;
gchar *input;
IdeDocumentationContext context;
GPtrArray *proposals;
};
G_DEFINE_TYPE (IdeDocumentationInfo, ide_documentation_info, G_TYPE_OBJECT)
enum {
PROP_0,
PROP_INPUT,
PROP_CONTEXT,
LAST_PROP
};
static GParamSpec *properties [LAST_PROP];
IdeDocumentationInfo *
ide_documentation_info_new (const gchar *input,
IdeDocumentationContext context)
{
return g_object_new (IDE_TYPE_DOCUMENTATION_INFO,
"input", input,
"context", context,
NULL);
}
void
ide_documentation_info_take_proposal (IdeDocumentationInfo *self,
IdeDocumentationProposal *proposal)
{
g_return_if_fail (IDE_IS_DOCUMENTATION_INFO (self));
g_return_if_fail (IDE_IS_DOCUMENTATION_PROPOSAL (proposal));
g_ptr_array_add (self->proposals, proposal);
}
/**
* ide_documentation_info_get_proposal:
* @self: An #IdeDocumentationInfo
* @index: the number of the proposal
*
* Requests proposal for the index.
*
* Returns: (transfer none): An #IdeDocumentationProposal
*/
IdeDocumentationProposal *
ide_documentation_info_get_proposal (IdeDocumentationInfo *self,
guint index)
{
g_return_val_if_fail (IDE_IS_DOCUMENTATION_INFO (self), NULL);
g_return_val_if_fail (self->proposals != NULL, NULL);
g_return_val_if_fail (self->proposals->len > index, NULL);
return g_ptr_array_index (self->proposals, index);
}
static void
ide_documentation_info_finalize (GObject *object)
{
IdeDocumentationInfo *self = (IdeDocumentationInfo *)object;
g_clear_pointer (&self->input, g_free);
g_clear_pointer (&self->proposals, g_ptr_array_unref);
G_OBJECT_CLASS (ide_documentation_info_parent_class)->finalize (object);
}
gchar *
ide_documentation_info_get_input (IdeDocumentationInfo *self)
{
g_return_val_if_fail (IDE_IS_DOCUMENTATION_INFO (self), NULL);
return self->input;
}
IdeDocumentationContext
ide_documentation_info_get_context (IdeDocumentationInfo *self)
{
g_return_val_if_fail (IDE_IS_DOCUMENTATION_INFO (self), IDE_DOCUMENTATION_CONTEXT_NONE);
return self->context;
}
guint
ide_documentation_info_get_size (IdeDocumentationInfo *self)
{
g_return_val_if_fail (IDE_IS_DOCUMENTATION_INFO (self), 0);
return self->proposals != NULL ? self->proposals->len : 0;
}
static void
ide_documentation_info_set_input (IdeDocumentationInfo *self,
const gchar *input)
{
g_return_if_fail (IDE_IS_DOCUMENTATION_INFO (self));
g_return_if_fail (self->input == NULL);
self->input = g_strdup (input);
}
static void
ide_documentation_info_set_context (IdeDocumentationInfo *self,
IdeDocumentationContext context)
{
g_return_if_fail (IDE_IS_DOCUMENTATION_INFO (self));
g_return_if_fail (self->context == IDE_DOCUMENTATION_CONTEXT_NONE);
self->context = context;
}
static void
ide_documentation_info_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
IdeDocumentationInfo *self = IDE_DOCUMENTATION_INFO (object);
switch (prop_id)
{
case PROP_INPUT:
g_value_set_string (value, ide_documentation_info_get_input (self));
break;
case PROP_CONTEXT:
g_value_set_int (value, ide_documentation_info_get_context (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
ide_documentation_info_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
IdeDocumentationInfo *self = IDE_DOCUMENTATION_INFO (object);
switch (prop_id)
{
case PROP_INPUT:
ide_documentation_info_set_input (self, g_value_get_string (value));
break;
case PROP_CONTEXT:
ide_documentation_info_set_context (self, g_value_get_int (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
ide_documentation_info_class_init (IdeDocumentationInfoClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = ide_documentation_info_finalize;
object_class->get_property = ide_documentation_info_get_property;
object_class->set_property = ide_documentation_info_set_property;
properties [PROP_INPUT] =
g_param_spec_string ("input",
"Input",
"Input",
NULL,
(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
properties [PROP_CONTEXT] =
g_param_spec_int ("context",
"Context",
"Context",
IDE_DOCUMENTATION_CONTEXT_NONE,
IDE_DOCUMENTATION_CONTEXT_LAST,
IDE_DOCUMENTATION_CONTEXT_NONE,
(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_properties (object_class, LAST_PROP, properties);
}
static void
ide_documentation_info_init (IdeDocumentationInfo *self)
{
self->proposals = g_ptr_array_new_with_free_func (g_object_unref);
self->context = IDE_DOCUMENTATION_CONTEXT_NONE;
self->input = NULL;
}
/* ide-documentation-info.h
*
* Copyright (C) 2017 Lucie Charvat <luci.charvat@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef IDE_DOCUMENTATION_INFO_H
#define IDE_DOCUMENTATION_INFO_H
#include "ide-documentation-proposal.h"
G_BEGIN_DECLS
#define IDE_TYPE_DOCUMENTATION_INFO (ide_documentation_info_get_type())
G_DECLARE_FINAL_TYPE (IdeDocumentationInfo, ide_documentation_info, IDE, DOCUMENTATION_INFO, GObject)
typedef enum {
IDE_DOCUMENTATION_CONTEXT_NONE,
IDE_DOCUMENTATION_CONTEXT_CARD_C,
IDE_DOCUMENTATION_CONTEXT_LAST,
} IdeDocumentationContext;
IdeDocumentationInfo *ide_documentation_info_new (const gchar *input,
IdeDocumentationContext context);
void ide_documentation_info_take_proposal (IdeDocumentationInfo *self,
IdeDocumentationProposal *proposal);
IdeDocumentationContext ide_documentation_info_get_context (IdeDocumentationInfo *self);
gchar *ide_documentation_info_get_input (IdeDocumentationInfo *self);
guint ide_documentation_info_get_size (IdeDocumentationInfo *self);
IdeDocumentationProposal *ide_documentation_info_get_proposal (IdeDocumentationInfo *self,
guint index);
G_END_DECLS
#endif /* IDE_DOCUMENTATION_INFO_H */
/* ide-documentation-proposal.c
*
* Copyright (C) 2017 Lucie Charvat <luci.charvat@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define G_LOG_DOMAIN "ide-documentation-proposal"
#include "ide-documentation-proposal.h"
typedef struct
{
gchar *header;
gchar *text;
gchar *uri;
} IdeDocumentationProposalPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (IdeDocumentationProposal, ide_documentation_proposal, G_TYPE_OBJECT)
enum {
PROP_0,
PROP_HEADER,
PROP_TEXT,
PROP_URI,
LAST_PROP
};
static GParamSpec *properties [LAST_PROP];
IdeDocumentationProposal *
ide_documentation_proposal_new (const gchar *uri)
{
return g_object_new (IDE_TYPE_DOCUMENTATION_PROPOSAL,
"uri", uri,
NULL);
}
const gchar *
ide_documentation_proposal_get_header (IdeDocumentationProposal *self)
{
IdeDocumentationProposalPrivate *priv = ide_documentation_proposal_get_instance_private (self);
g_return_val_if_fail (IDE_IS_DOCUMENTATION_PROPOSAL (self), NULL);
return priv->header;
}
const gchar *
ide_documentation_proposal_get_text (IdeDocumentationProposal *self)
{
IdeDocumentationProposalPrivate *priv = ide_documentation_proposal_get_instance_private (self);
g_return_val_if_fail (IDE_IS_DOCUMENTATION_PROPOSAL (self), NULL);
return priv->text;
}
const gchar *
ide_documentation_proposal_get_uri (IdeDocumentationProposal *self)
{
IdeDocumentationProposalPrivate *priv = ide_documentation_proposal_get_instance_private (self);
g_return_val_if_fail (IDE_IS_DOCUMENTATION_PROPOSAL (self), NULL);
return priv->uri;
}
void
ide_documentation_proposal_set_header (IdeDocumentationProposal *self,
const gchar *header)
{
IdeDocumentationProposalPrivate *priv = ide_documentation_proposal_get_instance_private (self);
g_return_if_fail (IDE_IS_DOCUMENTATION_PROPOSAL (self));
if (g_strcmp0 (priv->header, header) != 0)
{
g_free (priv->header);
priv->header = g_strdup (header);
g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_HEADER]);
}
}
void
ide_documentation_proposal_set_text (IdeDocumentationProposal *self,
const gchar *text)
{
IdeDocumentationProposalPrivate *priv = ide_documentation_proposal_get_instance_private (self);
g_return_if_fail (IDE_IS_DOCUMENTATION_PROPOSAL (self));
if (g_strcmp0 (priv->text, text) != 0)
{
g_free (priv->text);
priv->text = g_strdup (text);
g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TEXT]);
}
}
void
ide_documentation_proposal_set_uri (IdeDocumentationProposal *self,
const gchar *uri)
{
IdeDocumentationProposalPrivate *priv = ide_documentation_proposal_get_instance_private (self);
g_return_if_fail (IDE_IS_DOCUMENTATION_PROPOSAL (self));
priv->uri = g_strdup (uri);
}
static void
ide_documentation_proposal_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
IdeDocumentationProposal *self = IDE_DOCUMENTATION_PROPOSAL (object);
switch (prop_id)
{
case PROP_HEADER:
g_value_set_string (value, ide_documentation_proposal_get_header (self));
break;
case PROP_TEXT:
g_value_set_string (value, ide_documentation_proposal_get_text (self));
break;
case PROP_URI:
g_value_set_string (value, ide_documentation_proposal_get_uri (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
ide_documentation_proposal_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
IdeDocumentationProposal *self = IDE_DOCUMENTATION_PROPOSAL (object);
switch (prop_id)
{
case PROP_HEADER:
ide_documentation_proposal_set_header (self, g_value_get_string (value));
break;
case PROP_TEXT:
ide_documentation_proposal_set_text (self, g_value_get_string (value));
break;
case PROP_URI:
ide_documentation_proposal_set_uri (self, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
ide_documentation_proposal_finalize (GObject *object)
{
IdeDocumentationProposal *self = (IdeDocumentationProposal *)object;
IdeDocumentationProposalPrivate *priv = ide_documentation_proposal_get_instance_private (self);
g_clear_pointer (&priv->header, g_free);
g_clear_pointer (&priv->text, g_free);
g_clear_pointer (&priv->uri, g_free);
G_OBJECT_CLASS (ide_documentation_proposal_parent_class)->finalize (object);
}
static void
ide_documentation_proposal_class_init (IdeDocumentationProposalClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = ide_documentation_proposal_finalize;
object_class->get_property = ide_documentation_proposal_get_property;
object_class->set_property = ide_documentation_proposal_set_property;
properties [PROP_HEADER] =
g_param_spec_string ("header",
"Header",
"Header",
NULL,
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
properties [PROP_TEXT] =
g_param_spec_string ("text",
"Text",
"Text",
NULL,
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
properties [PROP_URI] =
g_param_spec_string ("uri",
"Uri",
"Uri",
NULL,
(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
g_object_class_install_properties (object_class, LAST_PROP, properties);
}
void
ide_documentation_proposal_init (IdeDocumentationProposal *self)
{
IdeDocumentationProposalPrivate *priv = ide_documentation_proposal_get_instance_private (self);
priv->header = NULL;
priv->text = NULL;
priv->uri = NULL;
}
/* ide-documentation-proposal.h
*
* Copyright (C) 2017 Lucie Charvat <luci.charvat@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef IDE_DOCUMENTATION_PROPOSAL_H
#define IDE_DOCUMENTATION_PROPOSAL_H
#include "ide-object.h"
G_BEGIN_DECLS
#define IDE_TYPE_DOCUMENTATION_PROPOSAL (ide_documentation_proposal_get_type())
struct _IdeDocumentationProposalClass
{
GObjectClass parent_class;
};
typedef struct _IdeDocumentationProposalClass IdeDocumentationProposalClass;
G_DECLARE_DERIVABLE_TYPE (IdeDocumentationProposal, ide_documentation_proposal, IDE, DOCUMENTATION_PROPOSAL, GObject)
IdeDocumentationProposal *ide_documentation_proposal_new (const gchar *url);
void ide_documentation_proposal_set_header (IdeDocumentationProposal *self,
const gchar *header);
void ide_documentation_proposal_set_text (IdeDocumentationProposal *self,
const gchar *text);
const gchar *ide_documentation_proposal_get_header (IdeDocumentationProposal *self);
const gchar *ide_documentation_proposal_get_text (IdeDocumentationProposal *self);
const gchar *ide_documentation_proposal_get_uri (IdeDocumentationProposal *self);
G_END_DECLS
#endif /* IDE_DOCUMENTATION_PROPOSAL_H */
/* ide-documentation-provider.c
*
* Copyright (C) 2017 Lucie Charvat <luci.charvat@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define G_LOG_DOMAIN "ide-documentation-provider"
#include "ide-documentation-provider.h"
G_DEFINE_INTERFACE (IdeDocumentationProvider, ide_documentation_provider, IDE_TYPE_OBJECT)
static void
ide_documentation_provider_default_init (IdeDocumentationProviderInterface *iface)
{
}
void
ide_documentation_provider_get_info (IdeDocumentationProvider *provider,
IdeDocumentationInfo *info)
{
g_return_if_fail (IDE_IS_DOCUMENTATION_PROVIDER (provider));
return IDE_DOCUMENTATION_PROVIDER_GET_IFACE (provider)->get_info (provider, info);
}
gchar *
ide_documentation_provider_get_name (IdeDocumentationProvider *provider)
{
g_return_val_if_fail (IDE_IS_DOCUMENTATION_PROVIDER (provider), NULL);
return IDE_DOCUMENTATION_PROVIDER_GET_IFACE (provider)->get_name (provider);
}
IdeDocumentationContext
ide_documentation_provider_get_context (IdeDocumentationProvider *provider)
{
g_return_val_if_fail (IDE_IS_DOCUMENTATION_PROVIDER (provider), IDE_DOCUMENTATION_CONTEXT_NONE);
return IDE_DOCUMENTATION_PROVIDER_GET_IFACE (provider)->get_context (provider);
}
/* ide-documentation-provider.h
*
* Copyright (C) 2017 Lucie Charvat <luci.charvat@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef IDE_DOCUMENTATION_PROVIDER_H
#define IDE_DOCUMENTATION_PROVIDER_H
#include <gtksourceview/gtksource.h>
#include "ide-documentation-info.h"
G_BEGIN_DECLS
#define IDE_TYPE_DOCUMENTATION_PROVIDER (ide_documentation_provider_get_type())
G_DECLARE_INTERFACE (IdeDocumentationProvider, ide_documentation_provider, IDE, DOCUMENTATION_PROVIDER, IdeObject)
struct _IdeDocumentationProviderInterface
{
GTypeInterface parent_interface;
void (*get_info) (IdeDocumentationProvider *self,
IdeDocumentationInfo *info);
gchar *(*get_name) (IdeDocumentationProvider *self);
IdeDocumentationContext (*get_context) (IdeDocumentationProvider *self);
};
gchar *ide_documentation_provider_get_name (IdeDocumentationProvider *self);
void ide_documentation_provider_get_info (IdeDocumentationProvider *self,
IdeDocumentationInfo *info);
IdeDocumentationContext ide_documentation_provider_get_context (IdeDocumentationProvider *self);
G_END_DECLS
#endif /* IDE_DOCUMENTATION_PROVIDER_H */
/* ide-documentation.c
*
* Copyright (C) 2017 Lucie Charvat <luci.charvat@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib/gi18n.h>
#include <libpeas/peas.h>
#include "ide-documentation.h"
#include "ide-documentation-provider.h"
#include "ide-documentation-proposal.h"
struct _IdeDocumentation
{
GObject parent_instance;
PeasExtensionSet *extensions;
};
G_DEFINE_TYPE (IdeDocumentation, ide_documentation, IDE_TYPE_OBJECT)
static void
ide_documentation_finalize (GObject *object)
{
IdeDocumentation *self = (IdeDocumentation *)object;
g_clear_object (&self->extensions);
G_OBJECT_CLASS (ide_documentation_parent_class)->finalize (object);
}
static void
ide_documentation_search_foreach (PeasExtensionSet *set,
PeasPluginInfo *plugin_info,
PeasExtension *exten,
gpointer user_data)
{
IdeDocumentationProvider *provider = (IdeDocumentationProvider *) exten;
IdeDocumentationInfo *info = user_data;
if (ide_documentation_provider_get_context (provider) == ide_documentation_info_get_context (info))
ide_documentation_provider_get_info (provider, info);