Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » appending text programmatically.
appending text programmatically. [message #692379] Mon, 04 July 2011 09:58 Go to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Hello there, Sorry for this trivial question, but I coudn't find a
method to append text to the xtext document programmatically.

How do I do this?

IXtextDocument doc = editor.getDocument();
doc.?

thx Christophe
Re: appending text programmatically. [message #692383 is a reply to message #692379] Mon, 04 July 2011 10:39 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

doesn't doc.set(doc.get()+ whateverToAppend) work?

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: appending text programmatically. [message #692392 is a reply to message #692383] Mon, 04 July 2011 10:54 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
On 04-07-11 12:39, Alexander Nittka wrote:
> Hi,
>
> doesn't doc.set(doc.get()+ whateverToAppend) work?
>
> Alex
thanks, I actually resolved by this:

IXtextDocument doc = editor.getDocument();
int len = doc.getLength();
try {
if( len == 0){
doc.set(toInsert);
}else{
doc.replace(len-1, toInsert.length(), toInsert);
}
} catch (BadLocationException e) {
e.printStackTrace();
}
Re: appending text programmatically. [message #692393 is a reply to message #692392] Mon, 04 July 2011 11:01 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Actuall this one is better, and doesn't replace the last char, and keeps
the cursor in place:

IXtextDocument doc = editor.getDocument();
int len = doc.getLength();
try {
if( len == 0){
doc.set(toInsert);
}else{
String lastChar = doc.get(len-1, 1);
doc.replace(len-1, toInsert.length(), lastChar + toInsert);
}
} catch (BadLocationException e) {
e.printStackTrace();
}


On 04-07-11 12:54, Christophe Bouhier wrote:
> On 04-07-11 12:39, Alexander Nittka wrote:
>> Hi,
>>
>> doesn't doc.set(doc.get()+ whateverToAppend) work?
>>
>> Alex
> thanks, I actually resolved by this:
>
> IXtextDocument doc = editor.getDocument();
> int len = doc.getLength();
> try {
> if( len == 0){
> doc.set(toInsert);
> }else{
> doc.replace(len-1, toInsert.length(), toInsert);
> }
> } catch (BadLocationException e) {
> e.printStackTrace();
> }
Re: appending text programmatically. [message #692520 is a reply to message #692393] Mon, 04 July 2011 16:23 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
In Xtext 1.0.x it was reccomended to do this using a unit of work; isn't
this necessary anymore?

On 07/04/2011 01:01 PM, Christophe Bouhier wrote:
> Actuall this one is better, and doesn't replace the last char, and keeps
> the cursor in place:
>
> IXtextDocument doc = editor.getDocument();
> int len = doc.getLength();
> try {
> if( len == 0){
> doc.set(toInsert);
> }else{
> String lastChar = doc.get(len-1, 1);
> doc.replace(len-1, toInsert.length(), lastChar + toInsert);
> }
> } catch (BadLocationException e) {
> e.printStackTrace();
> }
>
>
> On 04-07-11 12:54, Christophe Bouhier wrote:
>> On 04-07-11 12:39, Alexander Nittka wrote:
>>> Hi,
>>>
>>> doesn't doc.set(doc.get()+ whateverToAppend) work?
>>>
>>> Alex
>> thanks, I actually resolved by this:
>>
>> IXtextDocument doc = editor.getDocument();
>> int len = doc.getLength();
>> try {
>> if( len == 0){
>> doc.set(toInsert);
>> }else{
>> doc.replace(len-1, toInsert.length(), toInsert);
>> }
>> } catch (BadLocationException e) {
>> e.printStackTrace();
>> }
>


--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net


Re: appending text programmatically. [message #692543 is a reply to message #692520] Mon, 04 July 2011 17:36 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

changes to the model should still be done in a (modify) unit of work. The code responsible for applying quickfixes could serve as a guide. There you have a semantic version (modify the model) and a syntactic version (modify the document content).

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: appending text programmatically. [message #692604 is a reply to message #692543] Mon, 04 July 2011 20:22 Go to previous message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
On 04-07-11 19:36, Alexander Nittka wrote:
> Hi,
>
> changes to the model should still be done in a (modify) unit of work.
> The code responsible for applying quickfixes could serve as a guide.
> There you have a semantic version (modify the model) and a syntactic
> version (modify the document content).
>
> Alex
OK, thx BTW the code earlier doesn't work, as I missread the .replace API.

This is better:

private void insertKeyPadText(String toInsert) {
IXtextDocument doc = editor.getDocument();
int len = doc.getLength();
try {
doc.replace(len, 0, toInsert);
} catch (BadLocationException e) {
e.printStackTrace();
}
}

In a Unit of Work of course :)
Cheers Christophe.
Previous Topic:Supertype from referenced ecore model
Next Topic:[Xtext] Processing Xtext Model
Goto Forum:
  


Current Time: Thu Mar 28 10:57:36 GMT 2024

Powered by FUDForum. Page generated in 0.03530 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top