Gem #91: Smart Completion (Part 2 of 2)

by Quentin Ochem —AdaCore

Let's get started...

In this Gem, we're going to fill in additional parts of the Ada source files created in Part 1 of this series on using GPS's smart completion mechanism. All the semantic information required to provide this computation is done on the fly. Note that the features described in this Gem are not yet available, and will be released as a part of the GPS version following 4.4.0. GNAT Pro supported customers can request early access to a GPS version with these capabilities.

The completion mechanism relies on a parser launched at GPS startup. The progress of the parser will appear on the bottom right corner of the GPS window. Be aware that completion may not be accurate before the parsing is finished.

Completion of aggregates

GPS can automatically complete qualified aggregates. Instead of writing a series of assignments to individual components of a variable, let's try automatically initializing the value as a whole. Type "A_Variable := Rec'(". The left parenthesis is an automatic trigger for the completion mechanism. The first entry offers the choice of filling in all the fields for that record in a named aggregate -- only the component values need to be provided by the user. Other entries offer the choice of adding a new name in the list of values for the aggregate.

Completion of pragmas and attributes

Let's say we want to add an assertion before a call to Arctan, checking that the argument is greater than or equal to zero.

Just before the statement containing the call, type "pragma". The smart completion feature will be triggered automatically. The list of all available pragmas is shown, with their associated documentation. Scroll down to Assert. You now have access to the documentation for the Assert pragma. Press enter once you've finished viewing the documentation, and complete it, for example: "pragma Assert (X >= 0)".

Attributes can be listed the same way. For example, by typing X', smart completion is triggered, and all available attributes are listed.

Completion of generic entities

Let's consider an instance of an Ada container in our application. Start with a simple main subprogram:

with Ada.Containers.Doubly_Linked_Lists;
use Ada.Containers;

package Main is
   type Rec is record
      A, B, C : Integer;
   end record;
begin
   null;
end Main;

Then, in the declarations, enter: "package R_List is new Doubly_Linked_Lists (". Smart completion is triggered, and you can complete the formal part. Let's just choose Element_Type here, and provide Rec as the actual parameter. Add a use clause on that package. You should now have:

package R_List is new Ada.Containers.Doubly_Linked_Lists (Element_Type => Rec);
use R_List;

Declare a variable of that list kind, such as "L : R_List.List;". Then in the sequence of statements, after the begin, add a few elements, for instance "L.Append (Rec'(0, 0, 0));". Then try to access the first element. Type "L.First_Element.". The completion feature understands the generic instantiation and offers to complete the selected name with one of the three fields, A, B, or C.

This completes our small tutorial on using the GPS smart completion capability. As mentioned, the features presented in this Part 2 Gem will be available in the upcoming release of GPS (version 5.0.0).


About the Author

Quentin Ochem has a software engineering background, specialized in software development for critical applications. He has over 10 years of experience in Ada development. He works today as a technical account manager for AdaCore, following projects related to avionics, railroad, space and defense industries. He also teaches the avionics standard DO-178B course at the EPITA University in Paris.