From f9430c09eacdab62b55fe05d0553ec334cc6a3ec Mon Sep 17 00:00:00 2001 From: Truetsch Uwe Date: Wed, 20 Sep 2023 15:36:55 +0000 Subject: [PATCH 1/4] fix brace alignment check on module level --- lint.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lint.go b/lint.go index d67be6e9..b3f22200 100644 --- a/lint.go +++ b/lint.go @@ -226,7 +226,6 @@ func lint(cmd *cobra.Command, args []string) error { for _, def := range tree.Modules() { mod := def.Node.(*syntax.Module) - if isWhiteListed(style.Ignore.Modules, syntax.Name(mod.Name)) { continue } @@ -236,6 +235,8 @@ func lint(cmd *cobra.Command, args []string) error { ccID := syntax.Node(mod) caseElse := make(map[syntax.Node]int) + checkNaming(mod, style.Naming.Modules) + checkBraces(mod.LBrace, mod.RBrace) mod.Inspect(func(n syntax.Node) bool { if n == nil { stack = stack[:len(stack)-1] @@ -248,10 +249,6 @@ func lint(cmd *cobra.Command, args []string) error { case *syntax.Ident: checkUsage(n) - case *syntax.Module: - checkNaming(n, style.Naming.Modules) - checkBraces(n.LBrace, n.RBrace) - case *syntax.FuncDecl: ccID = n cc[ccID] = 1 // Intial McCabe value From 21b05138055b8b24b4f1a4c7f2250d3d3d9e8fd5 Mon Sep 17 00:00:00 2001 From: Truetsch Uwe Date: Wed, 20 Sep 2023 16:00:49 +0000 Subject: [PATCH 2/4] Missing file names in error message --- error.go | 20 ++++++++++---------- main.go | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/error.go b/error.go index c90d0262..abadaa62 100644 --- a/error.go +++ b/error.go @@ -19,7 +19,7 @@ type errPattern struct { } func (e errPattern) Error() string { - return fmt.Sprintf("%s: error: %s", syntax.Begin(e.node), e.msg) + return fmt.Sprintf("%s:%s: error: %s", syntax.Filename(e.node), syntax.Begin(e.node), e.msg) } func (e errPattern) IsSilent() bool { return isSilent(e.node, "TemplateDef") } @@ -30,8 +30,8 @@ type errLines struct { } func (e errLines) Error() string { - return fmt.Sprintf("%s: error: %q must not have more than %d lines (%d)", - syntax.Begin(e.node), syntax.Name(e.node), style.MaxLines, e.lines) + return fmt.Sprintf("%s:%s: error: %q must not have more than %d lines (%d)", + syntax.Filename(e.node), syntax.Begin(e.node), syntax.Name(e.node), style.MaxLines, e.lines) } func (e errLines) IsSilent() bool { return isSilent(e.node, "CodeStatistics.TooLong") } @@ -41,8 +41,8 @@ type errBraces struct { } func (e errBraces) Error() string { - return fmt.Sprintf("%s: error: braces must be in the same line or same column", - syntax.Begin(e.right)) + return fmt.Sprintf("%s:%s: error: braces must be in the same line or same column", + syntax.Filename(e.right), syntax.Begin(e.right)) } type errComplexity struct { @@ -51,8 +51,8 @@ type errComplexity struct { } func (e errComplexity) Error() string { - return fmt.Sprintf("%s: error: cyclomatic complexity of %q (%d) must not be higher than %d", - syntax.Begin(e.node), syntax.Name(e.node), e.complexity, style.Complexity.Max) + return fmt.Sprintf("%s:%s: error: cyclomatic complexity of %q (%d) must not be higher than %d", + syntax.Filename(e.node), syntax.Begin(e.node), syntax.Name(e.node), e.complexity, style.Complexity.Max) } func (e errComplexity) IsSilent() bool { return isSilent(e.node, "CodeStatistics.TooComplex") } @@ -62,7 +62,7 @@ type errMissingCaseElse struct { } func (e errMissingCaseElse) Error() string { - return fmt.Sprintf("%s: error: missing case else in select statement", syntax.Begin(e.node)) + return fmt.Sprintf("%s:%s: error: missing case else in select statement", syntax.Filename(e.node), syntax.Begin(e.node)) } type errUsageExceedsLimit struct { @@ -73,8 +73,8 @@ type errUsageExceedsLimit struct { } func (e errUsageExceedsLimit) Error() string { - return fmt.Sprintf("%s: error: %q must not be used more than %d times. %s", - syntax.Begin(e.node), syntax.Name(e.node), e.limit, e.text) + return fmt.Sprintf("%s:%s: error: %q must not be used more than %d times. %s", + syntax.Filename(e.node), syntax.Begin(e.node), syntax.Name(e.node), e.limit, e.text) } type errUnusedModule struct { diff --git a/main.go b/main.go index 9cf036c5..68cdfb67 100644 --- a/main.go +++ b/main.go @@ -82,7 +82,7 @@ var ( if path, err := exec.LookPath("k3-" + args[0]); err == nil { return proc.Exec(path, args[1:]...) } - return fmt.Errorf("unknown command: %s", args[0]) + return fmt.Errorf("ntt: unknown command: %s", args[0]) } if err := cmd.Flags().Parse(args); err != nil { From c1a9140ef9416d7724971cc9e9a5f1abab167200 Mon Sep 17 00:00:00 2001 From: Luczak Kamil Date: Tue, 26 Sep 2023 14:26:15 +0000 Subject: [PATCH 3/4] Add diagnostics flag to package.yml --- cmake/FindNTT.cmake | 14 +++++++++++++- project/project.go | 3 +++ project/project_test.go | 8 ++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/cmake/FindNTT.cmake b/cmake/FindNTT.cmake index b7b48bc2..e5b60599 100644 --- a/cmake/FindNTT.cmake +++ b/cmake/FindNTT.cmake @@ -50,6 +50,7 @@ manifest. [PARAMETERS_FILE file] [WORKING_DIRECTORY dir] [TARGETS target1...] + [DIAGNOSTICS flags] ) ``add_ttcn3_suite`` creates a target TGT and test suite manifest @@ -93,6 +94,9 @@ manifest. Add additional target-level dependencies. This is used to assure SUTs are built before a test is executed. + ``DIAGNOSTICS flags`` + Specifies the compiler diagnostics flags + ]====================================================================] if(TTCN3_PROTOBUF_INCLUDED) @@ -134,7 +138,7 @@ file(REMOVE "${CMAKE_BINARY_DIR}/ttcn3_suites.json") function(add_ttcn3_suite TGT) set("ARGS_PREFIX" "") set("ARGS_OPTIONS" "") - set("ARGS_ONE_VALUE" "NAME;TIMEOUT;HOOKS_FILE;PARAMETERS_FILE;WORKING_DIRECTORY") + set("ARGS_ONE_VALUE" "NAME;TIMEOUT;HOOKS_FILE;PARAMETERS_FILE;WORKING_DIRECTORY;DIAGNOSTICS") set("ARGS_MULTI_VALUE" "VARS;SOURCES;DEPENDS;TARGETS") cmake_parse_arguments("${ARGS_PREFIX}" "${ARGS_OPTIONS}" "${ARGS_ONE_VALUE}" "${ARGS_MULTI_VALUE}" ${ARGN}) @@ -215,6 +219,14 @@ function(add_ttcn3_suite TGT) add_dependencies("${TGT}" "${x}") endforeach() + if (_DIAGNOSTICS) + string(REPLACE " " ";" _DIAGNOSTICS ${_DIAGNOSTICS}) + string(APPEND MANIFEST "diagnostics:\n") + foreach(x ${_DIAGNOSTICS}) + string(APPEND MANIFEST " - ${x}\n") + endforeach() + endif() + file(GENERATE OUTPUT "${MANIFEST_FILE}" CONTENT "${MANIFEST}") __ntt_add_db_entry("${CMAKE_BINARY_DIR}/ttcn3_suites.json" "${TGT}" "${_WORKING_DIRECTORY}" "${CMAKE_CURRENT_LIST_DIR}") diff --git a/project/project.go b/project/project.go index 1a29e88d..d244d28c 100644 --- a/project/project.go +++ b/project/project.go @@ -129,6 +129,9 @@ type Manifest struct { // this variables section. Variables env.Env + // Diagnostics is a list of diagnostics flags used by compilator + Diagnostics []string `json:"diagnostics"` + // Parameters is an embedded parameters file. Parameters `json:",inline"` diff --git a/project/project_test.go b/project/project_test.go index affdfede..9f55a3e2 100644 --- a/project/project_test.go +++ b/project/project_test.go @@ -281,6 +281,14 @@ func TestWithManifest(t *testing.T) { assert.Nil(t, err) assert.Equal(t, "https://file.txt", c.HooksFile) }) + t.Run("diagnostics", func(t *testing.T) { + c, err := manifest("foo/bar/package.yml", ` + diagnostics: + - -w1 + - --white-list-dir=foo/bar/path`) + assert.Nil(t, err) + assert.Equal(t, []string([]string{"-w1", "--white-list-dir=foo/bar/path"}), c.Diagnostics) + }) t.Run("variables", func(t *testing.T) { c, err := manifest("foo/bar/package.yml", ` variables: From 0864aa7710aef85bab5f8323adacfee04c269b6a Mon Sep 17 00:00:00 2001 From: Truetsch Uwe Date: Thu, 5 Oct 2023 07:13:12 +0000 Subject: [PATCH 4/4] Remove unexpected characters from textDocument/hover output --- internal/lsp/hover.go | 18 ++++---- internal/lsp/hover_test.go | 84 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 internal/lsp/hover_test.go diff --git a/internal/lsp/hover.go b/internal/lsp/hover.go index e2e69d83..fffc5d58 100644 --- a/internal/lsp/hover.go +++ b/internal/lsp/hover.go @@ -54,18 +54,18 @@ func getSignature(def *ttcn3.Node) string { switch node := def.Node.(type) { case *syntax.FuncDecl: sig.WriteString(node.Kind.String() + " " + node.Name.String()) - sig.Write(content[node.Params.Pos()-1 : node.Params.End()]) + sig.Write(content[node.Params.Pos() : node.Params.End()]) if node.RunsOn != nil { sig.WriteString("\n ") - sig.Write(content[node.RunsOn.Pos()-1 : node.RunsOn.End()]) + sig.Write(content[node.RunsOn.Pos() : node.RunsOn.End()]) } if node.System != nil { sig.WriteString("\n ") - sig.Write(content[node.System.Pos()-1 : node.System.End()]) + sig.Write(content[node.System.Pos() : node.System.End()]) } if node.Return != nil { sig.WriteString("\n ") - sig.Write(content[node.Return.Pos()-1 : node.Return.End()]) + sig.Write(content[node.Return.Pos() : node.Return.End()]) } case *syntax.ValueDecl, *syntax.TemplateDecl, *syntax.FormalPar, *syntax.StructTypeDecl, *syntax.ComponentTypeDecl, *syntax.EnumTypeDecl, *syntax.PortTypeDecl: sig.Write(content[def.Node.Pos()-1 : def.Node.End()]) @@ -85,6 +85,10 @@ func getSignature(def *ttcn3.Node) string { } func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*protocol.Hover, error) { + return ProcessHover(params, &s.db, s.clientCapability.HoverContent) +} + +func ProcessHover(params *protocol.HoverParams, db *ttcn3.DB, capability HoverContentProvider) (*protocol.Hover, error) { var ( file = string(params.TextDocument.URI.SpanURI()) line = int(params.Position.Line) + 1 @@ -101,13 +105,13 @@ func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*prot return nil, nil } - for _, def := range tree.LookupWithDB(x, &s.db) { + for _, def := range tree.LookupWithDB(x, db) { defFound = true comment = syntax.Doc(def.Node) signature = getSignature(def) if tree.Root != def.Root { - posRef = s.clientCapability.HoverContent.LinkForNode(def) + posRef = capability.LinkForNode(def) } } if !defFound { @@ -122,7 +126,7 @@ func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*prot } } - hoverContents := s.clientCapability.HoverContent.Print(signature, comment, posRef) + hoverContents := capability.Print(signature, comment, posRef) hover := &protocol.Hover{Contents: hoverContents} return hover, nil diff --git a/internal/lsp/hover_test.go b/internal/lsp/hover_test.go new file mode 100644 index 00000000..a6b0a800 --- /dev/null +++ b/internal/lsp/hover_test.go @@ -0,0 +1,84 @@ +package lsp_test + +import ( + "fmt" + "testing" + + "github.com/nokia/ntt/internal/fs" + "github.com/nokia/ntt/internal/lsp" + "github.com/nokia/ntt/internal/lsp/protocol" + "github.com/nokia/ntt/ttcn3" + "github.com/stretchr/testify/assert" +) + +func TestPlainTextHoverForFunction(t *testing.T) { + actual := testHover(t, ` + module Test { + function myfunc(integer x) + runs on Component + system System + return integer + { + return x; + } + }`, + protocol.Position{Line: 2, Character: 25}, + &lsp.PlainTextHover{}) + + expected := + "function myfunc(integer x)\n" + + " runs on Component\n" + + " system System\n" + + " return integer\n" + + assert.Equal(t, expected, actual.Contents.Value) +} + +func TestMarkdownHoverForFunction(t *testing.T) { + actual := testHover(t, ` + module Test { + function myfunc(integer x) + runs on Component + system System + return integer + { + return x; + } + }`, + protocol.Position{Line: 2, Character: 25}, + &lsp.MarkdownHover{}) + + expected := + "```typescript\n" + + "function myfunc(integer x)\n" + + " runs on Component\n" + + " system System\n" + + " return integer\n" + + "```\n" + + " - - -\n" + + "\n" + + " - - -\n" + + assert.Equal(t, expected, actual.Contents.Value) +} + +func testHover(t *testing.T, text string, position protocol.Position, capability lsp.HoverContentProvider) *protocol.Hover { + t.Helper() + + file := fmt.Sprintf("%s.ttcn3", t.Name()) + fs.SetContent(file, []byte(text)) + + params := protocol.HoverParams{ + TextDocumentPositionParams: protocol.TextDocumentPositionParams{ + Position: position, + TextDocument: protocol.TextDocumentIdentifier{ + URI: protocol.DocumentURI(file), + }, + }, + } + + hover, err := lsp.ProcessHover(¶ms, &ttcn3.DB{}, capability) + assert.Equal(t, err, nil) + + return hover +}