Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Help with labeling features
Help with labeling features [message #1832422] Thu, 17 September 2020 11:17 Go to next message
Eclipse UserFriend
Hello everyone,

A colleague and I are working on developing a DSL with Xtext, and one of the things we are trying to modify is the labeling capabilities of our language server. We are using our language server in a separate text editor through the LSP, and when we are editing a file, we have access to an outline view that displays the names for each element in the parse tree. The problem is that each of these element's name is effectively a single integer, so the outline view is populated entirely by numbers.

We want to make this outline view more verbose by adding strings of text to provide more information to the user. Rather than just display "1", we would like to display "surface 1". We have tried to do this by creating customized versions of the SimpleNameLabelProvider and DocumentSymbolMapper java files and placing them in the IDE source folder. However, after making some changes and binding our custom files to the IDE Module file, we have noticed that there weren't any changes.

Are we going about this in the right manner? We have also noticed that there was an absence of the NameLabelProvider and DocumentSymbolMapper files in the file explorer before we added the custom files. Is it possible that we may need to generate these default files before they are customized and overwritten in the IDE folder?

Edit: These are the files I am referring to, straight from the Xtext github.
* Document Symbol Mapper file: https://github.com/eclipse/xtext-core/blob/master/org.eclipse.xtext.ide/src/org/eclipse/xtext/ide/server/symbol/DocumentSymbolMapper.java
* Simple Name Label provider: https://github.com/eclipse/xtext-core/blob/master/org.eclipse.xtext.ide/src/org/eclipse/xtext/ide/labels/SimpleNameLabelProvider.java
* Hierarchical Document Symbol Service, which I believe gets the symbols from the Document Symbol mapper file: https://github.com/eclipse/xtext-core/blob/master/org.eclipse.xtext.ide/src/org/eclipse/xtext/ide/server/symbol/HierarchicalDocumentSymbolService.java

Thanks in advanced.

[Updated on: Thu, 17 September 2020 11:20] by Moderator

Re: Help with labeling features [message #1832423 is a reply to message #1832422] Thu, 17 September 2020 11:20 Go to previous messageGo to next message
Eclipse UserFriend
simply creating new (sub)classes and binding them should do the trick.

e.g

	def Class<? extends DocumentSymbolNameProvider> bindDocumentSymbolNameProvider() {
		return CustomDocumentSymbolNameProvider
	}

[Updated on: Thu, 17 September 2020 11:41] by Moderator

Re: Help with labeling features [message #1832435 is a reply to message #1832423] Thu, 17 September 2020 17:54 Go to previous messageGo to next message
Eclipse UserFriend
Christian,

This binding would take place in the IdeModule extend file correct?
Re: Help with labeling features [message #1832443 is a reply to message #1832435] Fri, 18 September 2020 01:34 Go to previous messageGo to next message
Eclipse UserFriend
yes
Re: Help with labeling features [message #1832493 is a reply to message #1832443] Fri, 18 September 2020 19:56 Go to previous messageGo to next message
Eclipse UserFriend
Thank you for clarifying the file location.

I am still having some difficulty trying to implement the labeling. For example, in the customized NameLabelProvider, we want to change how the name labels are returned, so it looks something like this:

	@Override
	public String getNameLabel(Object element) {
		if (element instanceof EObject) {
			String name = SimpleAttributeResolver.NAME_RESOLVER.apply(((EObject) element));
			String type = element.getClass().toString();
			String label = type + name;
			return label


After which the IdeModule would look like this:

public Class<? extends SimpleNameLabelProvider> bindSimpleNameLabelProvider() {
		return customNameLabelProvider.class;
	}


Is this the correct approach?

Additionally, there is no need to add any other stubs or code in the Grammar runtime module to do these types of changes, right? I just wanted to be sure I didn't need to generate default name label provider files before doing these types of modifications.

Thanks.
Re: Help with labeling features [message #1832503 is a reply to message #1832493] Sat, 19 September 2020 01:57 Go to previous messageGo to next message
Eclipse UserFriend
i dont understand

I just wanted to be sure I didn't need to generate default name label provider files before doing these types of modifications.

there are no stubs generated never
so what you do looks fine
Re: Help with labeling features [message #1832654 is a reply to message #1832503] Tue, 22 September 2020 17:12 Go to previous messageGo to next message
Eclipse UserFriend
Quote:
there are no stubs generated never
so what you do looks fine


Thank you for the info so far. We have tried to make these modification but nothing is changing for us when we use our language server in another text editor.

My colleague has added some code to write to an external file in the IdeModule as well as in the CustomNameLabelProvider for the purpose of verifying that these files are being used. He has noted that the IdeModule file does write to an external file whereas the CustomNameLabelProvider does not. Does this suggest anything? We think that the customNameLabelProvider is not being used properly by the IdeModule, and as a result that is why we are not seeing any changes to the outline view. It this line of reasoning correct?

As an additional point, a while back I was able to implement a labeling modification through the UI package which changed the outline view in an eclipse plug in only. So I think the general process of modifying the labels should be somewhat similar. We are hoping to effectively make the same type of modification through the IDE folders.

Thanks
Re: Help with labeling features [message #1832660 is a reply to message #1832654] Wed, 23 September 2020 01:18 Go to previous messageGo to next message
Eclipse UserFriend
Can you please show what you exactly did. (Code snippets, screenshot of project etc)
E.g. what is an external file
Re: Help with labeling features [message #1832708 is a reply to message #1832660] Wed, 23 September 2020 12:40 Go to previous messageGo to next message
Eclipse UserFriend
Yes. So the following code was placed in the custom NameLabelProvider java file which is meant to override the SimpleNameLabelProvider. This code is meant to write a message to an external text file as well as modify the names of the element objects.
@Override
    public String getNameLabel(Object element) {
       
        BufferedWriter out = null;

        try {
            FileWriter fstream = new FileWriter("F:\\Research\\Workbench\\out.txt", true); //true tells to append data.
            out = new BufferedWriter(fstream);
            out.write("I'm in the custom name label provider\n");
        }

        catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }

       
        if (element instanceof EObject) {
            String name = SimpleAttributeResolver.NAME_RESOLVER.apply(((EObject) element));
            String type = element.getClass().toString();
            String ideName = type + name;
            try {
                out.write(ideName + "\n");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return ideName;
            //return SimpleAttributeResolver.NAME_RESOLVER.apply(((EObject) element));
        } else if (element instanceof IEObjectDescription) {
            String name = ((IEObjectDescription) element).getName().getLastSegment();
            String type = element.getClass().toString();
            String ideName = type + name;
            try {
                out.write(ideName + "\n");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return ideName;
            //return ((IEObjectDescription) element).getName().getLastSegment();
           
        }
        return null;
    }

The other modification is to the IdeModule java file, which binds the custom NameLabelProvider. This one also writes a message to an external file as well.
public Class<customNameLabelProvider> bindSimpleNameLabelProvider() throws IOException{
        BufferedWriter out = null;

        try {
            FileWriter fstream = new FileWriter("F:\\Research\\Workbench\\out.txt", true); //true tells to append data.
            out = new BufferedWriter(fstream);
            out.write("The binding did stuff\n");
        }

        catch (IOException e) {
            out.write("Error: " + e.getMessage() + "\n");
        }

        finally {
            if(out != null) {
                out.close();
            }
        }
        return customNameLabelProvider.class;
    }


From what we can see, the code in the IdeModule does appear to write a message to that specified external text file, which suggests that it is doing something but we think it is not doing anything with the customNameLabelProvider file as that file is not providing any feedback.

Thanks for your time.
Re: Help with labeling features [message #1832713 is a reply to message #1832708] Wed, 23 September 2020 14:08 Go to previous messageGo to next message
Eclipse UserFriend
i dont understand what you do with that bind.

the signature should be

public Class<? extends SimpleNameLabelProvider> bindSimpleNameLabelProvider() {
return CustomNameLabelProvider.class;
}

please post the complete IdeModule Class
Re: Help with labeling features [message #1832714 is a reply to message #1832713] Wed, 23 September 2020 15:21 Go to previous messageGo to next message
Eclipse UserFriend
This is what the IdeModuleClass currently looks like:
class McnpIdeModule extends AbstractMcnpIdeModule {
	public Class<CustomNameLabelProvider> bindSimpleNameLabelProvider() throws IOException{

        BufferedWriter out = null;

        try {
            FileWriter fstream = new FileWriter("F:\\Research\\Workbench\\out.txt", true); //true tells to append data.
            out = new BufferedWriter(fstream);
            out.write("The binding did stuff\n");
        }

        catch (IOException e) {
            out.write("Error: " + e.getMessage() + "\n");
        }

        finally {
            if(out != null) {
                out.close();
            }
        }
        return customNameLabelProvider.class;
    }
}


The code in the IdeModule class has some statements to write a string to an external text file, that is purely to see if the module class is doing something. So the IdeModule Class should look like this?
class McnpIdeModule extends AbstractMcnpIdeModule {
	public Class<? extends SimpleNameLabelProvider> bindSimpleNameLabelProvider() {
		return CustomNameLabelProvider.class;
	}
}


Assuming that the CustomNameLabelProvider.java file looks like the code in the previous comment.
Re: Help with labeling features [message #1832715 is a reply to message #1832713] Wed, 23 September 2020 15:39 Go to previous messageGo to next message
Eclipse UserFriend
ahhhhhhhh. INameLabalProvider / SimpleNameLabelProvider is not used in LSP but in web only
what is used is

DocumentSymbolMapper.DocumentSymbolNameProvider (as stated in my initial answer)
the names are too similar though

=>
public class CustomDocumentSymbolNameProvider extends DocumentSymbolMapper.DocumentSymbolNameProvider {
	@Override
	public String getName(EObject object) {
		String r  = super.getName(object);
		return r == null ? null : r + "Lala";
	}
}


public class MyDslIdeModule extends AbstractMyDslIdeModule {
	public Class<? extends DocumentSymbolMapper.DocumentSymbolNameProvider> bindDocumentSymbolMapper$DocumentSymbolNameProvider() {
		return CustomDocumentSymbolNameProvider.class;
	}
}


package org.xtext.example.mydsl.tests

import org.eclipse.lsp4j.ClientCapabilities
import org.eclipse.lsp4j.DocumentSymbolCapabilities
import org.eclipse.lsp4j.InitializeParams
import org.eclipse.lsp4j.TextDocumentClientCapabilities
import org.eclipse.xtext.testing.AbstractLanguageServerTest
import org.junit.jupiter.api.Test

class DocumentSymbolTest extends AbstractLanguageServerTest {

	new() {
		super("mydsl")
	}

	static val (InitializeParams)=>void INITIALIZER = [
		capabilities = new ClientCapabilities() => [
			textDocument = new TextDocumentClientCapabilities() => [
				documentSymbol = new DocumentSymbolCapabilities() => [
					it.hierarchicalDocumentSymbolSupport = true;
				];
			];
		];
	];

	@Test
	def void testDocumentSymbol_01() {
		testDocumentSymbol[
			initializer = INITIALIZER
			model = '''
				Hello A!
				Hello B!
			'''
			expectedSymbols = '''
				symbol "ALala" {
				    kind: 7
				    range: [[0, 0] .. [0, 8]]
				    selectionRange: [[0, 6] .. [0, 7]]
				    details: 
				    deprecated: false
				}
				symbol "BLala" {
				    kind: 7
				    range: [[1, 0] .. [1, 8]]
				    selectionRange: [[1, 6] .. [1, 7]]
				    details: 
				    deprecated: false
				}

			'''
		]

	}

}



[Updated on: Wed, 23 September 2020 15:57] by Moderator

Re: Help with labeling features [message #1832732 is a reply to message #1832715] Wed, 23 September 2020 22:45 Go to previous message
Eclipse UserFriend
That did the trick! Thank you for your help.
Previous Topic:Xtext document set content does not update the model immediately
Next Topic:Problem with Maven-Tycho build of Xtext project in GitLab CI
Goto Forum:
  


Current Time: Wed Jul 23 10:47:13 EDT 2025

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

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

Back to the top